Note that there are some explanatory texts on larger screens.

plurals
  1. POModels and Listboxes
    primarykey
    data
    text
    <p>Ok so i'm trying to use a viewmodel with a list box such that when i add items to the listbox and then post the form, those items are carried over through the viewmodel to the action.</p> <p>ViewModel:</p> <pre><code>public virtual ICollection&lt;Sku&gt; Skus { get; set; } //This is this items sku's public virtual ICollection&lt;Sku&gt; AllSkus { get; set; } //This is all sku's </code></pre> <p>My Listbox looks like this: </p> <pre><code>@Html.ListBoxFor(m=&gt;m.Skus, new MultiSelectList(Model.Skus, "Id", "Name"), new { id = "skuList", style="width: 200px; height: 100px" }) </code></pre> <p>I have a table full of all skus that when i select one, it adds to the list box using jquery. (I'll post all that code below)</p> <p>My receiving action (skus is null):</p> <pre><code> [HttpPost] public ActionResult AddProductOffering(ProductOfferingModel model) { return View(model); } </code></pre> <p>The items are adding to the listbox just fine, however the model is still null after the post.</p> <p>Clearly i am just updating the listbox and not the model, i do not know how to tie the two together in this way.</p> <p>Any help would be much appreciated, thank you!</p> <p>(Updated) Full .cshtml file:</p> <pre><code>@using Web.ControlPanel.Helpers @model Web.ControlPanel.ViewModels.Offering.ProductOfferingModel @using (Html.BeginForm()) { &lt;h2&gt;Product Offering&lt;/h2&gt; &lt;br /&gt; @Html.LabelFor(m =&gt; m.Name) @Html.EditorFor(m =&gt; m.Name) &lt;br /&gt; &lt;br /&gt; &lt;label&gt;Offerings Skus&lt;/label&gt;&lt;br/&gt; @Html.ListBoxFor(m=&gt;m.Skus, new MultiSelectList(Model.Skus, "Id", "Name"), new { id = "skuList", style="width: 200px; height: 100px" }) &lt;br/&gt;&lt;br/&gt; &lt;button class="button" type="submit" value="Save"&gt;Save&lt;/button&gt; &lt;br/&gt;&lt;br/&gt;&lt;br/&gt; &lt;h3&gt;All Skus&lt;/h3&gt; &lt;p&gt; Search: @Html.TextBox("skuFilter") &lt;input type="submit" value="Search Skus" name="search" /&gt; &lt;/p&gt; &lt;table class="onepx" style="width: 80%"&gt; &lt;tr&gt; &lt;td colspan="4" style="text-align: right;"&gt; @Html.Pager("List", Model.SkuPager, 3, (string)ViewBag.SkuFilter) &lt;/td&gt; &lt;/tr&gt; &lt;thead&gt; &lt;tr style="font-weight: bold;"&gt; &lt;td&gt;Product&lt;/td&gt; &lt;td&gt;Sku&lt;/td&gt; &lt;td&gt;List Price&lt;/td&gt; &lt;td&gt;MSRP&lt;/td&gt; &lt;td&gt;Action&lt;/td&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody&gt; @if (Model.Skus != null) { foreach (var sku in Model.AllSkus) { &lt;tr&gt; &lt;td&gt; @Html.DisplayFor(m =&gt; sku.Product.Name) &lt;/td&gt; &lt;td&gt; &lt;label id="skuName@(sku.Id)"&gt;@sku.Name&lt;/label&gt; &lt;/td&gt; &lt;td&gt;$@Html.DisplayFor(m =&gt; sku.ListPrice) &lt;/td&gt; &lt;td&gt;$@Html.DisplayFor(m =&gt; sku.Msrp) &lt;/td&gt; &lt;td&gt; &lt;a href="#" class="AddSku" id="addSku@(sku.Id)"&gt;Add To Offering&lt;/a&gt; &lt;/td&gt; &lt;/tr&gt; } } &lt;/tbody&gt; &lt;tfoot&gt; &lt;tr&gt; &lt;td colspan="4" style="text-align: right;"&gt; @Html.Pager("List", Model.SkuPager, 3, (string)ViewBag.SkuFilter) &lt;/td&gt; &lt;/tr&gt; &lt;/tfoot&gt; &lt;/table&gt; } @section Scripts { &lt;script&gt; $(document).ready(function() { //Array of skus var skus = new Array(); $('.AddSku').click(function(e) { e.preventDefault(); var id = e.target.id.replace("addSku", ""); var exists = false; for (var i = 0; i &lt; skus.length; i++) { if (skus[i] == id) { exists = true; } } if (!exists) { var name = $("#skuName" + id).text(); $('#skuList').append('&lt;option value=' + id + '&gt;' + name + '&lt;/option&gt;'); skus.push(id); } }); }); &lt;/script&gt; } </code></pre>
    singulars
    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.
 

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