2014-01-23 48 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)