0

I hope anyone can help me I am trying to submit a single object from a list, but I can't seem to get any information over.

@using System.Activities.Statements
@using NET_MVC_Application.Models

@{
    ViewBag.Title = "Index";
}
@model TweakersRemake.Models.ProductViewModel
<h2>Index</h2>

<div>


        @using (Html.BeginForm("Prijzen", "Product", FormMethod.Post))
    { 
         foreach (Product p in Model.Products)
            {

              <div>
                  @{
                      Product Pro = new Product();
                      Pro = p;
                  }
                  @Html.Hidden("Id",Pro.Id)
                  @Html.LabelFor(m => Pro.Catergorie) : @Html.DisplayFor(m => Pro.Catergorie) <br/>
                  @Html.LabelFor(m => Pro.Naam) : @Html.DisplayFor(m => Pro.Naam)<br/>
                  @Html.LabelFor(m => Pro.Foto_Url) : @Html.DisplayFor(m => Pro.Foto_Url)<br/>
                  <button type="submit">Prijzen</button>
              </div>
              }

          }
</div>

This I my layout, I Can send the individual information Like that Id but not the product, I also used p as the model inside the Html helpers

    [HttpPost]
    public ActionResult Prijzen(ProductViewModel model, int? Id, Product Pro)
    {

        return View(model.Products.Find(m => m.Id == Id));
    }

So I am trying to get the single product, I hope anyone can help me.

  • 1
    I think if you move the Html.BeginFrom inside the for-each it should work. Sorry no vs here to test. off-topic: ambitious project 'TweakersRemake' ;) – Florian Schaal Jun 06 '16 at 21:04
  • Already tried that, but it didn't work. But you're right it should be inside – Mitch Kuijpers Jun 06 '16 at 21:08
  • Do you get any error? Can you describe what exactly happens when you try it in the foreach? – Florian Schaal Jun 06 '16 at 21:09
  • I can send the Httppost, But the object will just be empty/ Except the Id which I specified inside the html.hidden("Id", Pro.Id) I can probably Do the same for the rest of the variables, But I should be able to construct that Object – Mitch Kuijpers Jun 06 '16 at 21:16
  • Can I ask you to try and replace `@Html.Hidden("Id",Pro.Id)` with `` and see if that goes through to the controller? – A3mercury Jun 06 '16 at 21:21
  • That does work, but The @Html.Hidden("Id",Pro.Id) worked as well, so I could try to To just do it manually by naming all the Labels. But I will probably not be able to send The ProductViewModel right? Which is a list of Products. – Mitch Kuijpers Jun 06 '16 at 21:34
  • 1
    Your loop is not creating any form controls except for property `Id` and if your were it would be creating `name` attributes which have no relationship to your model, and would be generating invalid html because of duplicate `id` attrbutes (you cannot use a `foreach` loop to generate form controls for a collection (refer [this answer](http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable/30094943#30094943)). –  Jun 06 '16 at 23:39
  • But what are you trying to achieve with this anyway - even if you created the `name` attributes correctly, it would only ever post back the 1st item in the collection so the loop is pointless. And since you clearly are not editing any properties of the collection - why would you degrade performance by sending extra html to the client and then posting it all back unchanged? –  Jun 06 '16 at 23:42

1 Answers1

0

If you're looking to persist the data of the ViewModel through postback, you need to set hidden elements for each of the items you want persisted. You won't be able to use the foreach loop for this in the View, instead, use a regular for loop in the following way:

for(int i = 0; i < Model.Products.Count; i++)
{
    @Html.LabelFor(model => model.Products[i].Property)
    @Html.DisplayFor(model => model.Products[i].Property)
    @Html.HiddenFor(model => model.Products[i].Property)
}

And you can receive a List<ProductsViewModel> in your controller action method for the POST verb. public ActionResult Prijzen(List<ProductsViewModel> model... )

Ingenioushax
  • 718
  • 5
  • 20