2014-01-23 51 views
1

在我的MVC4应用程序中,我有一个管理部分。在这个管理员视图中,我可以看到所有未注册的用户。这个观点也是一个可编辑的编辑器。为了有一个IEnumerable一个编辑,我已经添加的IList和环在我看来是这样的:用于MVC4模型绑定的IList

@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" /> 
} 

但现在的问题是我不能使用Html.DisplayNameFor和Html.LabelFor。我能做些什么来正确显示标签?

回答

1

如果你真的不关心IList<T>,您可以将模型更改为IEnumerable<T>DisplayNameFor方法对IEnumerable过载,这意味着你将能够做到这一点:

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

LabelFor没有这种超载,因为label应该用于特定的元素,如:

@Html.LabelFor(modelItem => Model[i].UserName) 

编辑:

如果你婉发布这些数据来控制,更好地创造一个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 IEnumerable<PlusPlatform.Models.UserProfile> 
@using (Html.BeginForm()) 
{ 
    <table> 
     <tr> 
      <th> 
       @Html.DisplayNameFor(model => model.UserName) 
      </th> 
      ... 
     </tr> 
     @Html.EditorForModel() 
    </table> 
} 
+0

我关心IList,因为当我使用'IEnumerable '时,在将表单提交给控制器时,模型将为空。 – devqon

+0

@ user3153169,请参阅编辑的答案。 – Zabavsky

0

尝试这样

@using (Html.BeginForm()) 
{ 
    <table> 
     <tr> 
      <th> 
       <[email protected](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> 
} 
+0

我知道这工作正常,但这不是一个坏习惯吗?假设我改变模型的显示名称,那么标签不会更新。 – devqon

+0

这是如何回答这个问题的? OP显然希望从数据注释中获益。 – Zabavsky

+0

@ user3153169检查已编辑的答案 – Nilesh

0

如果你能以这种方式使用EditorFor,那么为什么不DisplayFor? 试试这个

@Html.LabelFor(modelItem => Model[0].FirstName)