Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC 3 Post of Viewmodel with Completex IEnumerable
    primarykey
    data
    text
    <p>I have a complex class that is part of a property of a viewmodel. My viewmodel has a wine class property and a wine class has a ICollection property called CaseProductions. The CaseProduction class has several properties as well. </p> <p>On the create GET event, the NewWineViewModel is instantiated, then it runs a GetCaseProductionDefaults with create a list of CaseProduction classes that have some default values, but are mostly empty. </p> <p>Now, I originally used razor to do a foreach statement and just pop out my table the way I wanted it. But I've see around that doesn't work to bind this type of IEnumerable back to the viewmodel on POST. I've tried to use the below, but no dice. </p> <pre><code>EditorFor(m =&gt; m.Wine.CaseProductions) </code></pre> <p>I'm really looking for advise on what the best way to handle this is. Each wine will have a collection of caseproductions, and I want that to bind back to the wine within the viewmodel. Is their some way I can edit the ids of those elements in razor to make sure they bind? What's the best way to handle this one? </p> <p>viewmodel:</p> <pre><code> public class NewWineViewModel { public Wine Wine { get; set; } public VOAVIRequest VOAVIRequest { get; set; } public bool IsRequest { get; set; } public Dictionary&lt;int, int&gt; 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; } } </code></pre> <p>case production class:</p> <pre><code>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; } } </code></pre> <p>getting default case productions:</p> <pre><code>public List&lt;CaseProduction&gt; GetCaseProductionDefaults(vfContext db) { //creates blank list of all formats List&lt;CaseProduction&gt; cp = new List&lt;CaseProduction&gt;(); 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; } </code></pre> <p>my razor code for the case production table:</p> <pre><code> @foreach (vf2.Models.CaseProduction cp in Model.Wine.CaseProductions) { &lt;tr&gt; &lt;td&gt;@cp.CaseProductionSize.Name &lt;/td&gt; &lt;td&gt;@Html.Raw(@Html.TextBoxFor(m =&gt; m.Wine.CaseProductions.Where(c =&gt; c.CaseProductionSizeID == cp.CaseProductionSizeID).First().CaseCount, new { @class = "caseCount", id = "txt" + cp.CaseProductionSize.Name }).ToString().Replace("CaseCount","txt" + cp.CaseProductionSize.Name)) &lt;/td&gt; &lt;td&gt; @Html.DropDownListFor(m =&gt; m.Wine.CaseProductions.Where(c =&gt; 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}) &lt;/td&gt; &lt;td class="totalBottleCalc"&gt; &lt;/td&gt; &lt;/tr&gt; } </code></pre> <p>instantiation of my caseproduction collection:</p> <pre><code> public ActionResult Create(int ID = 0, int VintUpID = 0) { NewWineViewModel nw = new NewWineViewModel(); nw.Wine.CaseProductions = nw.GetCaseProductionDefaults(db); nw.BottlesPerCase = nw.GetBottlesPerCase(db); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload