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
}
DisplayFor()does not create inputs for postback. You need to use methods that generate<input>or<select>elements such asEditorFor(),TextBoxFor(),HiddenFor()....for(int i = 0; i < Model.Projects.Count; i++) { @Html.HiddenFor(m => m.Projects[i].Name) ...But why on earth are you posting back a whole lot of hidden inputs?$.get()function to pass a value(s) to an action method, and that method calls the database to filter the results based on the value(s) and return a partial view that can be updated in the view - i.e. you don't need to reconstruct the whole page.