I have created a list of items from a database. These are shown correctly. Now i would like to save the changed booleans to my database. How can I receive all these items to my controller so i can loop through them by foreach or something?
CONTROLLER
public class ItemController : Controller
{
private SocialGEOContext db = new SocialGEOContext();
public ActionResult ApproveItems()
{
Utility ut = new Utility();
IEnumerable<Item> items;
items = ut.GetAllDistrictItems();
return View(items);
}
[HttpPost]
public ActionResult ApproveItems( ??? )
{
???
}
In the HttpPost i tried
[HttpPost]
public ActionResult ApproveItems( IEnumerable<Item> ViewItems )
{
foreach (Item item in ViewItems)
{
var testje = item.ItemTitle;
}
return View();
}
But the ViewItems is empty
PAGE
@model IEnumerable<LibModels.Item>
@{
ViewBag.Title = "ItemsByDistrict";
}
<h2>All items from district</h2>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<table>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.ItemID)
</td>
<td>
@Html.DisplayFor(modelItem => item.ItemTitle)
</td>
<td>
@Html.DisplayFor(modelItem => item.ItemCreateddate)
</td>
<td>
@Html.DisplayFor(modelItem => item.ItemDescription)
</td>
<td>
@Html.EditorFor(modelItem => item.ItemApproved)
</td>
</tr>
}
</table>
<p>
<input type="submit" value="Edit" />
</p>
</fieldset>
}
You need to use a for loop in order for the model binding to work
See this article for a good breakdown on model binding collections
Note: you’ll need to call
ToList()on the model so that you can use indexingIn other words in your action method:
And then change the model in the view: