Updating multiple items within same view
asp.net-mvc-3
Solution
It's hard to answer your question without model class, but idea is that your edit `inputs` must contain index in `name attribute`.
Something like this:
@for(int i = 0: i < Model.Count(); i++)
{
<tr>
<td>
@Html.DisplayFor(modelItem => Model[i].Item_Name)
</td>
<td>
@Html.DisplayFor(modelItem => Model[i].Wanted_Stock)
</td>
<td>
<div class="editor-field">
@Html.EditorFor(modelItem => Model[i].Actual_Stock)
@Html.ValidationMessageFor(modelItem => Model[i].Actual_Stock)
</div>
</td>
@Html.HiddenFor(modelItem => Model[i].ID)
</tr>
}
Added:
Sorry, thanks to Darin Dimitrov, you can't access `IEnumerable` by index, use `List` or `Array`.
Problem
I am trying to make a stock take application, My view loads all my stock with one editor. My controller is not getting any of the data from the view? I want to be able to edit all my stock at the same time? How can I do this Model Code ``` using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace FlatSystem.Models { public class Stock { public int ID { get; set; } public string Item_Name { get; set; } public int Actual_Stock { get; set; } public int Wanted_Stock { get; set; } } } ``` View Code ``` @model IEnumerable<FlatSystem.Models.Stock> @using (Html.BeginForm()) { @Html.ValidationSummary(true) <div class="sidemenu"> <div class="sidemenu-heading"> ReStock </div> <div class="div-body"> <table> <tr> <th> Item Name </th> <th> Wanted Stock </th> <th> Stock On Hand </th> <th></th> </tr> @foreach (var item in Model) { <tr> <td> @Html.DisplayFor(modelItem => item.Item_Name) </td> <td> @Html.DisplayFor(modelItem => item.Wanted_Stock) </td> <td> <div class="editor-field"> @Html.EditorFor(modelItem => item.Actual_Stock) @Html.ValidationMessageFor(modelItem => item.Actual_Stock) </div> </td> @Html.HiddenFor(modelItem => item.ID) </tr> } </table> </div> </div> <input type="submit" value="Submit" /> } ``` Controller Code ``` [HttpPost] public ActionResult ReStock(List<Stock> stock) { foreach (var item in stock) { if (ModelState.IsValid) { GR.InsertOrUpdate(item); } } GR.Save(); return RedirectToAction("Restock"); } ```