viewmodel 속성의 일부인 복잡한 클래스가 있습니다. 내 viewmodel에는 와인 클래스 속성이 있고 와인 클래스에는 CaseProductions라는 ICollection 속성이 있습니다. CaseProduction 클래스에는 여러 속성이 있습니다.MVC 3 Completex IEnumerable이 포함 된 Viewmodel의 게시물
GET 이벤트 생성시 NewWineViewModel이 인스턴스화 된 다음 일부 기본값이 있지만 대부분 비어있는 CaseProduction 클래스 목록을 생성하여 GetCaseProductionDefaults를 실행합니다.
이제는 원래 foreach 문을 수행하고 원하는 방식으로 내 테이블을 튀어 나오기 위해 면도기를 사용했습니다. 하지만 그 주위에 POST에 대한 viewmodel 다시 IEnumerable이이 유형을 바인딩하려면 작동하지 않는 것을 보았다. 나는 아래 주사위를 사용하려했지만 주사위는 사용하지 않았다.
EditorFor(m => m.Wine.CaseProductions)
나는 이것을 처리하는 가장 좋은 방법이 무엇인지에 대해 조언을 찾고 있습니다. 각각의 와인에는 케이스 제작 컬렉션이 있으며 뷰 모델 내 와인을 다시 바인딩해야합니다. 내가 묶는 지 확인하기 위해 면도날에있는 요소의 ID를 편집 할 수있는 방법이 있습니까? 이 것을 처리하는 가장 좋은 방법은 무엇입니까?
뷰 모델 :
public class NewWineViewModel
{
public Wine Wine { get; set; }
public VOAVIRequest VOAVIRequest { get; set; }
public bool IsRequest { get; set; }
public Dictionary<int, int> BottlesPerCase { get; set; }
public SelectList VarTypes { get; set; }
public SelectList Origins { get; set; }
public SelectList Apps { get; set; }
public SelectList Vintages { get; set; }
public SelectList Importers { get; set; }
}
경우 생산 클래스 :
public class CaseProduction
{
public int CaseProductionID { get; set; }
public int WineID { get; set; }
public int CaseProductionSizeID { get; set; }
public int CaseCount { get; set; }
public int CountPerCase { get; set; }
public virtual CaseProductionSize CaseProductionSize { get; set; }
public virtual Wine Wine { get; set; }
}
점점 기본 케이스 제작 :
public List<CaseProduction> GetCaseProductionDefaults(vfContext db)
{
//creates blank list of all formats
List<CaseProduction> cp = new List<CaseProduction>();
foreach (CaseProductionSize size in db.CaseProductionSizes)
{
int defaultBottlesPerCase = 1;
switch ((CaseProductionSizeEnum)size.CaseProductionSizeID)
{
case CaseProductionSizeEnum.s187ml:
defaultBottlesPerCase= 24;
break;
case CaseProductionSizeEnum.s375ml:
defaultBottlesPerCase = 12;
break;
case CaseProductionSizeEnum.s500ml:
defaultBottlesPerCase = 12;
break;
case CaseProductionSizeEnum.s750ml:
defaultBottlesPerCase = 12;
break;
default:
defaultBottlesPerCase = 1;
break;
}
cp.Add(new CaseProduction { CaseProductionSizeID = size.CaseProductionSizeID, CountPerCase = defaultBottlesPerCase, CaseProductionSize = size, WineID = this.Wine.WineID });
}
return cp;
}
경우 생산 테이블에 대한 내 면도기 코드 :
@foreach (vf2.Models.CaseProduction cp in Model.Wine.CaseProductions)
{
<tr>
<td>@cp.CaseProductionSize.Name
</td>
<td>@Html.Raw(@Html.TextBoxFor(m => m.Wine.CaseProductions.Where(c => c.CaseProductionSizeID == cp.CaseProductionSizeID).First().CaseCount, new { @class = "caseCount", id = "txt" + cp.CaseProductionSize.Name }).ToString().Replace("CaseCount","txt" + cp.CaseProductionSize.Name))
</td>
<td>
@Html.DropDownListFor(m => m.Wine.CaseProductions.Where(c => c.CaseProductionSizeID == cp.CaseProductionSizeID).First().CountPerCase, new SelectList(Model.BottlesPerCase, "Key", "Value", cp.CountPerCase), new { @class = "countPerCase", id = "ddl" + cp.CaseProductionSize.Name, name = "ddl" + cp.CaseProductionSize.Name})
</td>
<td class="totalBottleCalc">
</td>
</tr>
}
내 caseproduction 수집
인스턴스 :
이public ActionResult Create(int ID = 0, int VintUpID = 0)
{
NewWineViewModel nw = new NewWineViewModel();
nw.Wine.CaseProductions = nw.GetCaseProductionDefaults(db);
nw.BottlesPerCase = nw.GetBottlesPerCase(db);