I'm having these classes:
public class ProductViewModel
{
public IEnumerable<Product> Products { get; set; }
}
public class Product
{
public int ArticeNr { get; set; }
public string Name { get; set; }
}
And this view
@model ProductsViewModel
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
<table class="table">
@foreach (var item in Model.Products) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.ArticleNr)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
</tr>
}
</table>
<input type="submit" value="submit" name="submitButton" />
}
How can I pass All my values from Model.Products to the Controller? I want all my values in the foreach-loop sent to the controller
My controller is taking ProductsViewModel as parameter. But the values after I post in model.Products
is null.
public ActionResult Index(ProductsViewModel model) //here, model.Products is always null after post
{
//LOGIC
}
@Html.DisplayFor just displays the value in the view.
If you want the value to be passed back to the controller on post then you also need a @Html.HiddenFor
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With