Asp.net mvc MVC4中用于模型绑定的IList

Asp.net mvc MVC4中用于模型绑定的IList,asp.net-mvc,asp.net-mvc-4,razor,Asp.net Mvc,Asp.net Mvc 4,Razor,在我的MVC4应用程序中,我有一个管理部分。在这个管理员视图中,我可以看到所有(尚未)注册的未被接受的用户。此视图也是ienumerable的编辑器。为了拥有IEnumerable的编辑器,我在视图中添加了IList和for循环,如下所示: @model IList<PlusPlatform.Models.UserProfile> @{ ViewBag.Title = "Manage"; } <h2>@ViewBag.Title</h2> @usi

在我的MVC4应用程序中,我有一个管理部分。在这个管理员视图中,我可以看到所有(尚未)注册的未被接受的用户。此视图也是ienumerable的编辑器。为了拥有IEnumerable的编辑器,我在视图中添加了IList和for循环,如下所示:

@model IList<PlusPlatform.Models.UserProfile>

@{
    ViewBag.Title = "Manage";
}

<h2>@ViewBag.Title</h2>
@using (Html.BeginForm())
{
    <table>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.UserName)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.FirstName)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.LastName)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.DepartmentId)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.IsActivated)
            </th>
        </tr>

@for (var i = 0; i < Model.Count(); i++) {
    <tr>
        <td>
            @Html.EditorFor(modelItem => Model[i].UserName)
        </td>
        <td>
            @Html.EditorFor(modelItem => Model[i].FirstName)
        </td>
        <td>
            @Html.EditorFor(modelItem => Model[i].LastName)
        </td>
        <td>
            @Html.EditorFor(modelItem => Model[i].DepartmentId)
        </td>
        <td>
            @Html.EditorFor(modelItem => Model[i].IsActivated)
        </td>
    </tr>
}
</table>
<input type="submit" value="Save" />
}
@model-IList
@{
ViewBag.Title=“管理”;
}
@视图包。标题
@使用(Html.BeginForm())
{
@DisplayNameFor(model=>model.UserName)
@DisplayNameFor(model=>model.FirstName)
@DisplayNameFor(model=>model.LastName)
@DisplayNameFor(model=>model.DepartmentId)
@DisplayNameFor(model=>model.IsActivated)
@对于(var i=0;iModel[i].UserName)
@EditorFor(modelItem=>Model[i].FirstName)
@EditorFor(modelItem=>Model[i].LastName)
@EditorFor(modeleItem=>Model[i].DepartmentId)
@EditorFor(modelItem=>Model[i].IsActivated)
}
}
但是现在的问题是我不能使用Html.DisplayNameFor和Html.LabelFor。如何才能正确显示标签?

请这样尝试

@using (Html.BeginForm())
{
    <table>
        <tr>
            <th>
                <label@Html.DisplayNameFor(m =>m.First().UserName)</label>
            </th>
            <th>
                <label>@Html.DisplayNameFor(m =>m.First().FirstName)</label>
            </th>
            <th>
               <label>@Html.DisplayNameFor(m =>m.First().LastName)</label>
            </th>
            <th>
                <label>@Html.DisplayNameFor(m =>m.First().DepartmentId)</label>
            </th>
            <th>
               <label>@Html.DisplayNameFor(m =>m.First().IsActivated)</label>
            </th>
        </tr>

@for (var i = 0; i < Model.Count(); i++) {
    <tr>
        <td>
            @Html.EditorFor(modelItem => Model[i].UserName)
        </td>
        <td>
            @Html.EditorFor(modelItem => Model[i].FirstName)
        </td>
        <td>
            @Html.EditorFor(modelItem => Model[i].LastName)
        </td>
        <td>
            @Html.EditorFor(modelItem => Model[i].DepartmentId)
        </td>
        <td>
            @Html.EditorFor(modelItem => Model[i].IsActivated)
        </td>
    </tr>
}
@使用(Html.BeginForm())
{
m、 First().UserName)
@DisplayNameFor(m=>m.First().FirstName)
@DisplayNameFor(m=>m.First().LastName)
@DisplayNameFor(m=>m.First().DepartmentId)
@DisplayNameFor(m=>m.First().IsActivated)
@对于(var i=0;iModel[i].UserName)
@EditorFor(modelItem=>Model[i].FirstName)
@EditorFor(modelItem=>Model[i].LastName)
@EditorFor(modeleItem=>Model[i].DepartmentId)
@EditorFor(modelItem=>Model[i].IsActivated)
}

如果您并不真正关心
IList
,您可以将模型更改为
IEnumerable
。方法具有
IEnumerable
的重载,这意味着您可以执行以下操作:

@model IEnumerable<PlusPlatform.Models.UserProfile>
...
<th>
    @Html.DisplayNameFor(model => model.UserName)
</th>
编辑:

如果您想将该数据发布到控制器,最好创建一个
EditorTemplate
,将其命名为
UserProfile

@model PlusPlatform.Models.UserProfile
<tr>
    <td>
        @Html.EditorFor(m=> m.UserName)
    </td>
    <td>
        @Html.EditorFor(m=> m.FirstName)
    </td>
    <td>
        @Html.EditorFor(m=> m.LastName)
    </td>
    <td>
        @Html.EditorFor(m=> m.DepartmentId)
    </td>
    <td>
        @Html.EditorFor(m=> m.IsActivated)
    </td>
</tr>
@model PlusPlatform.Models.UserProfile
@EditorFor(m=>m.UserName)
@EditorFor(m=>m.FirstName)
@EditorFor(m=>m.LastName)
@EditorFor(m=>m.DepartmentId)
@EditorFor(m=>m.IsActivated)
然后在视图中去掉循环:

@model IEnumerable<PlusPlatform.Models.UserProfile>
@using (Html.BeginForm())
{
    <table>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.UserName)
            </th>
            ...
        </tr>
        @Html.EditorForModel()
    </table>
 }
@model IEnumerable
@使用(Html.BeginForm())
{
@DisplayNameFor(model=>model.UserName)
...
@Html.EditorForModel()
}

如果您可以用这种方式使用EditorFor,那么为什么不显示for呢? 试试这个


我知道这是正确的,但这不是一个坏的做法吗?假设我更改了模型的显示名称,那么标签就不会更新。这是如何回答问题的?OP显然想利用数据注释。我关心IList,因为当我使用
IEnumerable
时,将表单提交给电子控制器。
@model IEnumerable<PlusPlatform.Models.UserProfile>
@using (Html.BeginForm())
{
    <table>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.UserName)
            </th>
            ...
        </tr>
        @Html.EditorForModel()
    </table>
 }
  @Html.LabelFor(modelItem => Model[0].FirstName)