2

I have the following view ...

@model IEnumerable<Contact>

@{ ViewBag.Title = "Contact Manager"; }


    <table>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.FirstName)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.MiddleName)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.LastName)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.HomePhone)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.WorkPhone)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.MobilePhone)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.EMail)
            </th>
            <th></th>
        </tr>

    @*This first row is the search form*@

        <tr>
            <th>
                @Html.EditorFor(model => model.FirstName)
            </th>
            <th>
                @Html.EditorFor(model => model.MiddleName)
            </th>
            <th>
                @Html.EditorFor(model => model.LastName)
            </th>
            <th>
                @Html.EditorFor(model => model.HomePhone)
            </th>
            <th>
                @Html.EditorFor(model => model.WorkPhone)
            </th>
            <th>
                @Html.EditorFor(model => model.MobilePhone)
            </th>
            <th>
                @Html.EditorFor(model => model.EMail)
            </th>
            <th></th>
        </tr>

    </table>

The problem is that "model" parameter in the predicate argument to the "EditorFor()" calls, refers to the IEnumerable and not the individual Contact item the way that seems to in the "DisplayNameFor()" method. Therefore, I am receiving Compilation Errors because the property name (for example: "FirstName") is not a property of the IEnumerable.

Intellisense actually returns me various IEnumerable methods (Such as "Select()") for the "method" parameter used in the predicate argument to the function. Oddly enough, even though the "DisplayNameFor()" method calls appear to work, intellisense does not show properties of the Contact.

What's the difference here?

Honestly, it makes sense that the since the model is IEnumerable this syntax would not work. However I am confused as to why it DOES work for the "DisplayNameFor()" method which is inserted by the wizard when you generate a strongly typed view as a list. Then in this case, why would it work for one (DisplayNameFor()) ... but not the other (EditorFor()).

Thank you, G

4

2 回答 2

3

In order to edit fields, they have to refer to individual properties (so they can be model bound correctly). DisplayNameFor essentially gets the label for the first item in a collection.

You'll have to do it in a loop (a for loop if you're saving it, otherwise the fields won't be indexed correctly). Try this (you may have to make your model a List<T> though:

@for (int i = 0; i < Model.Count(); i++)
{
    <tr>
        <th>
            @Html.EditorFor(m => m[i].FirstName)
        </th>
        <th>
            @Html.EditorFor(m => m[i].MiddleName)
        </th>
        <th>
            @Html.EditorFor(m => m[i].LastName)
        </th>
        <th>
            @Html.EditorFor(m => m[i].HomePhone)
        </th>
        <th>
            @Html.EditorFor(m => m[i].WorkPhone)
        </th>
        <th>
            @Html.EditorFor(m => m[i].MobilePhone)
        </th>
        <th>
            @Html.EditorFor(m => m[i].EMail)
        </th>
        <th></th>
    </tr>
}
于 2013-04-28T22:27:05.173 回答
1

The reason is that DisplayNameFor doesn't rely on an instance. The DisplayName can just be obtained from inspecting the attributes of you Class whereas the EditorFor needs the actual data and thus needs an instance.

If you really are only binding to a single instance you should change your model:

@model Contact

The DisplayNameFor will keep working and your Editors will also start working

If you are binding to a list, you'll need to for-loop around your editors

于 2013-04-28T22:27:17.423 回答