Вопрос

The Model:

public class MyObject
{
    public IList<Entry> Entries;
}

public class Entry
{
    public string Name { get; set; }
}

If I use the default EditorFor(model => model.Entries) the name/id values are:

<input type="text" value="" name="Entries[0].Name" id="Entries_0__Name">

If instead I want to create an EditorFor template like this:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<List<CMS.Models.MyObject>>" %>

<div class="list">
<%
    for (int i = 0; i < Model.Count; i++)
    { %>
    <div class="object" id="<%: i %>">
        <%: Html.EditorFor(model => Model[i]) %>
        <%: Html.ValidationMessageFor(model => Model[i]) %>
    </div>
<%  } %>
</div>

The emitted name/id values are:

<input type="text" value="" name="Entries.[0].Name" id="Entries__0__Name">

So the name has a period between the property name and [0] and the id has an extra underscore to the left of the index number. This doesn't work with the default model binder. Is there a way to do this that does work with the default model binder?

Это было полезно?

Решение

I think you can do most of this using templates without having to explicitly iterate over the list, which seems to be causing your problems with the model binder.

Try making these shared editor templates:

MyObject Editor Template

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<CMS.Models.MyObject>" %>

<div class="list">
    <%: Html.EditorFor(m => m.Entries) %>
</div>

Entry Editor Template

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<CMS.Models.Entry>" %>

<div class="object" id="<%: ??? %>">
    <%: Html.EditorFor(m => m.Name) %>
    <%: Html.ValidationMessageFor(m => m.Name) %>
</div>

The only thing I haven't figured out yet is how to get the correct value in the id attribute of the div, hence the question marks. Final caveat is that I'm at home so I haven't actually tried this.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top