2

I've got an Index View with a table populated with data from a List property, but when I post the form with the table, the property is null.

Here is the Model:

public class BillViewModel
{

    public List<Product> ListProducts { get; set; }

}

Here is the product view:

    public class Product
{
    public int ProductId { get; set; }
    public string ProductName { get; set; }
    public decimal Price { get; set; }
}

Here is the View:

@using (Html.BeginForm())
{

<input type="submit" value="Aceptar"/>


<table id="tabla">


    @foreach (var item in Model.ListProducts)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.ProductName)
            </td>
        </tr>
    }

</table>
}

I can add or delete Products, but how can i get the product list in the controller?:

    [HttpPost]
    public ActionResult Index(BillViewModel billViewModel)
    {


        return View();
    }
2
  • 2
    Please, review this answer: stackoverflow.com/questions/17450772/… It's the same problem, I'm sure it will solve yours. Commented Jul 23, 2013 at 17:36
  • Yes you can get all details in your control. Mvc flow is that controller communicate with both modal and view. Commented Jul 23, 2013 at 17:37

1 Answer 1

4

That's quite easy, all you need to do is place all the item properties in the form, so something like:

<input type="submit" value="Aceptar"/>
<table id="tabla">
    @for (int i = 0; i < Model.ListProducts.Count; i++)
    {
        <tr>
            <td>
                @Html.HiddenFor(x => x.ListProducts[i].ProductId)
                @Html.HiddenFor(x => x.ListProducts[i].Price)
                @Html.DisplayFor(x => x.ListProducts[i].ProductName)
            </td>
        </tr>
    }
</table>
Sign up to request clarification or add additional context in comments.

5 Comments

Yes, I use a for instead a foreach giving an index to the list, to each property. All using a HiddenFor. And It works.
@DiegoUnanue Glad It does :) Feel free to mark it as an answer if it's what you're looking for :) Good luck. Cheers
What happens if I add a new row, for example i've got a textbox where i can add a new Product, this product isn't in the database so it hasn's id. What arquitectural aprouch can I use to send all products and insert into the database those that aren't in it. It have ProductId and ProductName, I can find those names that are not in the database. But if i can have repeated productNames?
@DiegoUnanue in your case if I'm supposed to add a new element I'd probably do an ajax post add a new element -> get back the result and add the new item along the rest on the form (following the same structure) this time however my item would have an Id. I hope this helps.
I was using foreach instead of for loop, which is why list was not submitting , changing it to for loop did it. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.