0

I have a view with an enumerable model. I'm trying to bind text boxes with the value of the instance of the object. Here is my view.

@model IEnumerable<SeniorProjectMVC.Models.Cart>

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h3>Your cart</h3>

@*<p>
    @Html.ActionLink("Create New", "Create")
</p>*@  

<div class="row">
    <div class="large-2 columns">
        <h4> @Html.DisplayNameFor(model => model.Quantity)</h4>
    </div>
    <div class="large-4 columns">
       <h4>@Html.DisplayNameFor(model => model.Sku.Product.Name)</h4> 
    </div>
    <div class="large-2 columns">
      <h4>@Html.DisplayNameFor(model => model.Sku.Product.Price)</h4>  
    </div>
    <div class="large-1 columns">
        <h4>In stock</h4>
    </div>
    <div class="large-3 columns">

    </div>
</div>

@foreach (var item in Model)
{
    <div class="row">
        <div class="large-2 columns">
            @Html.TextBox(item.Quantity.ToString())
        </div>
        <div class="large-4 columns">
            @Html.Display(item.Sku.Product.Name)
        </div>
        <div class="large-2 columns">
            @(item.Sku.Product.Price.ToString("c"))
        </div>
        <div class="large-1 columns">
            @(item.Sku.Quantity > 0 ? "In Stock" : "Out of stock")
        </div>
        <div class="large-3 columns">
            @Html.ActionLink("Edit", "Edit", new { id = item.ID }, new { @class = "button primary" }) |
            @Html.ActionLink("Details", "Details", new { id = item.ID }, new { @class = "button primary" }) |
            @Html.ActionLink("Delete", "Delete", new { id = item.ID }, new { @class = "button primary" })
        </div>
    </div>
}

The issue I seem to have run into is that the quantity won't populate in the textbox and it seems like the name for the product also isn't showing up. I know the Product Names have values, but they aren't displaying. Also, if I wanted to post back this collection in case of a value changing, what's the correct way to post back all the textbox values with their corresponding ID's so I can update the quantities in the cart that have changed.

ddeamaral
  • 1,403
  • 2
  • 28
  • 43

1 Answers1

0

So after some trial and error, I found the issue was I was using the wrong HTML helper overload. @Html.TextBox("Quantity", item.Quantity) is the correct way to output the value, and as for the name, the thing that worked for me is to do this @(item.Sku.Product.Name).

ddeamaral
  • 1,403
  • 2
  • 28
  • 43
  • For posting the collection, you cannot use a `foreach` loop. Refer [this answer](http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable/30094943#30094943) (and the correct usage for your current implementation which won't work would have been `@Html.TextBoxFor(m => item.Quantity)` - always use the strongly typed `HtmlHelper` methods) –  Aug 30 '16 at 21:44
  • So with my current markup, there isn't any way to post my quantities and sku id for each line item in my cart to my controller? – ddeamaral Aug 30 '16 at 22:55
  • No. Look at the html your generating - your have duplicate `name` attributes without indexers that are necessary to bind to a collection (and duplicate `id` attributes which is invalid html) - refer the link in my previous comment. –  Aug 30 '16 at 22:57