2014-01-23 3 views
1

MVC4 애플리케이션에는 관리자 파트가 있습니다. 이 관리자보기에서는 등록되지 않은 (아직) 승인 된 사용자를 모두 볼 수 있습니다. 이보기는 ienumerable의 편집기이기도합니다. 를 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을 사용할 수 있습니다. 라벨을 올바르게 표시하려면 어떻게해야합니까? 당신이 정말로 IList<T> 걱정하지 않는 경우

답변

1

같이하십시오. DisplayNameFor 방법은 IEnumerable에 대한 과부하를 가지고, 그건 당신이이 작업을 수행 할 수 있다는 것을 의미합니다 :

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

LabelForlabel 같은 특정 요소에 대해 사용되어야하기 때문에,이 오버로드를 가지고 있지 않습니다

@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> 

루프 제거 : 당신이 그런 식으로 EditorFor를 사용할 수있는 경우

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

IEist를 사용할 때, IEnumerable 을 사용할 때 컨트롤러에 양식을 제출할 때 Model이 null이되기 때문에 걱정하지 않습니다. – devqon

+0

@ user3153169, 수정 된 답변 참조 – Zabavsky

0

IEnumerable<T>에 모델을 변경할 수 있습니다,이

@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

관련 문제