2010-12-14 4 views
0

그래서 다른 ViewModels (PersonalIncomeViewModel) 컬렉션이있는 사용자 지정 ViewModel (CompanySalaryDataViewModel)이 있습니다. 궁극적으로, CompanySalaryDataViewModel에 대한 EditorTemplate을 가지고 싶습니다. PersonalIncomeViewModel에 대해 EditorTemplate을 사용하는 것은 어떻게 든 내 POST 컨트롤러로 다시 전달 될 때 내 ViewModel에 대한 양방향 바인딩을 제공합니다.컬렉션에 대해이 EditorTemplate을 어떻게 만들 수 있습니까?

public class CompanySalaryDataViewModel 
{ 
    string CompanyName { get; set; } 
    IList<PersonalIncomeViewModel> AllSalaryData { get; set; } 
} 

public class PersonalIncomeViewModel 
{ 
    long UniqueID { get; set; } 
    string PersonName { get; set; } 
    int JanIncome { get; set; } 
    int FebIncome { get; set; } 
    int MarIncome { get; set; } 
    int AprIncome { get; set; } 
    int MayIncome { get; set; } 
    int JunIncome { get; set; } 
    int JulIncome { get; set; } 
    int AugIncome { get; set; } 
    int SepIncome { get; set; } 
    int OctIncome { get; set; } 
    int NovIncome { get; set; } 
    int DecIncome { get; set; } 
} 

나는 이것이는 다음과 같은 마크 업 비슷하지만 작동하는 방식으로 수행합니다 :이와

<%= Html.TextBoxFor(x => x.CompanyName) %> 
    <table cellspacing="0"> 
     <thead> 
      <tr class="t-grid-header"> 
       <th class="t-header">ID</th> 
       <th class="t-header">Person</th> 
       <th class="t-header">Jan</th> 
       <th class="t-header">Feb</th> 
       <th class="t-header">Mar</th> 
       <th class="t-header">Apr</th> 
       <th class="t-header">May</th> 
       <th class="t-header">Jun</th> 
       <th class="t-header">Jul</th> 
       <th class="t-header">Aug</th> 
       <th class="t-header">Sep</th> 
       <th class="t-header">Oct</th> 
       <th class="t-header">Nov</th> 
       <th class="t-header">Dec</th> 
      </tr> 
     </thead> 
    <% 
    var isAlt = false; 
    foreach (var salaryData in Model.AllSalaryData) 
    { 
     var salary = salaryData; 
     if (isAlt) 
     {%> 
      <tr class="t-alt"> 
     <% 
     } 
     else 
     {%> 
      <tr> 
     <% 
     } 

     isAlt = !isAlt; 
     %> 
       <td><%=Html.TextBoxFor(x => salary.UniqueID)%></td> 
       <td><%=Html.TextBoxFor(x => salary.PersonName)%></td> 
       <td><%=Html.TextBoxFor(x => salary.JanIncome, new {id = salary.PersonName + "_1", style = "width: 45px;"})%></td> 
       <td><%=Html.TextBoxFor(x => salary.FebIncome, new {id = salary.PersonName + "_2", style = "width: 45px;"})%></td> 
       <td><%=Html.TextBoxFor(x => salary.MarIncome, new {id = salary.PersonName + "_3", style = "width: 45px;"})%></td> 
       <td><%=Html.TextBoxFor(x => salary.AprIncome, new {id = salary.PersonName + "_4", style = "width: 45px;"})%></td> 
       <td><%=Html.TextBoxFor(x => salary.MayIncome, new {id = salary.PersonName + "_5", style = "width: 45px;"})%></td> 
       <td><%=Html.TextBoxFor(x => salary.JunIncome, new {id = salary.PersonName + "_6", style = "width: 45px;"})%></td> 
       <td><%=Html.TextBoxFor(x => salary.JulIncome, new {id = salary.PersonName + "_7", style = "width: 45px;"})%></td> 
       <td><%=Html.TextBoxFor(x => salary.AugIncome, new {id = salary.PersonName + "_8", style = "width: 45px;"})%></td> 
       <td><%=Html.TextBoxFor(x => salary.SepIncome, new {id = salary.PersonName + "_9", style = "width: 45px;"})%></td> 
       <td><%=Html.TextBoxFor(x => salary.OctIncome, new {id = salary.PersonName + "_10", style = "width: 45px;"})%></td> 
       <td><%=Html.TextBoxFor(x => salary.NovIncome, new {id = salary.PersonName + "_11", style = "width: 45px;"})%></td> 
       <td><%=Html.TextBoxFor(x => salary.DecIncome, new {id = salary.PersonName + "_12", style = "width: 45px;"})%></td> 
      </tr> 
    <% 
    }%> 
    </table> 

문제를 예를 들어

,의는이 같은 뭔가 가정 해 봅시다 코드는 다음과 같습니다.

  1. 각 "January"텍스트 상자는 동일한 이름 (salary.JanIncome - 다른 급여와 구별 할 수있는 항목이 없습니다). 매월 같은 문제가 있습니다.
  2. 아마도 # 1 문제로 인해 게시 할 때 ViewModel에 다시 바인딩되지 않습니다. 내 AllSalaryData 컬렉션이 null입니다.
  3. 이 모든 것은 매우 비범 한 것으로 보이며이를 수행하는 더 좋은 방법이 있어야합니다.

궁극적으로, 나는 더 나은 컬렉션으로 템플릿을 사용하여 이해할 필요가 있지만, 그것의 다른 부분이 제대로 적절한 테이블 행에 t-alt 클래스를 적용 할 수있는 "논리"입니다 같아요.

참고 # 1 :이 텍스트 상자와 상호 작용하는 일부 자바 스크립트 항목에 대해 HTML ID를 설정합니다.

참고 # 2 : 나는 급여 데이터로 이것을 실제로하고 있지 않습니다.

+0

이상적이지 않은 기능 솔루션을 발견했지만 작동합니다. 분명히 'foreach'는 문제이지만, for (var i = 0; i Jaxidian

답변

0

그래, 이상적이지 않은 기능 솔루션을 발견했지만 작동합니다. 분명히 foreach은 문제이지만 for(var i=0; i < xxx.Count; i++)은 렌더링 된 컨트롤의 이름 생성에 인덱스 번호가 사용되기 때문에 작동합니다. 이상적이지는 않지만 완벽하게 작동합니다.

0

질문이 명확하지 않음 : 1. 'ViewModels'은 최소한 디자인 관점에서보기 모델입니다. 같은 개체 유형의 컬렉션을 보내기 위해 컬렉션/목록 또는 게시물을 '일괄 적으로'업데이트 할 수 있습니까? 2. 귀하의 모델 - PersonalIncome [ViewModel]이 지속 가능하다고 가정 해 봅시다. 따라서, 한 번에 하나의 레코드를 편집하게 될 것이며, 사용할 편집기 템플릿은 컬렉션의 항목 일뿐입니다.

당신이하고 싶은 것뿐만 아니라 의도를 자세히 설명 할 수 있습니까? 텍스트 상자로 '목록'보기를 만드는 것보다 다른 대안이있을 수 있습니다.

+0

대안은 선택 사항이 아닙니다. 비즈니스 요구 사항은 모든 이유를 고려하여 즉시 편집 할 수 있어야합니다. 데이터를 삽입하고 거대한 규모로 모든 텍스트 상자의 데이터로 자바 스크립트 수학을 수행하는 add'l 아약스 조회를 수행하는 AJAX 바인딩이있는 다른 그리드가 있지만 최종 사용자에게 최종 기능을 제공 할 수있는 능력이 필요합니다. - 최종 번호를 수정하십시오.이 동적으로 생성 된 "텍스트 상자 격자"에는 ~ 10 행만 있습니다. 그러나 입력은 대량으로 선택됩니다. 내가 제시 한 문제에 초점을 맞추면 모든 것을 무시할 수 있습니다. – Jaxidian

관련 문제