I\'m trying to make a website using asp.net mvc 4 & EF6 where I want to update multiple rows all at once. But for some reason, it\'s not workin
Your first problem is that your use of a foreach loop is generating duplicate name attributes, which will not bind to a collection, and as a result the BillLists parameter will always be an empty collection (its also generating duplicate id attributes which is invalid html). You need to use a for loop or a custom EditorTemplate for typeof BillCheck. Using a for loop, your view need to be
using (Html.BeginForm("MakeDue", "Home"))
{
@Html.ValidationSummary(true)
@for(int i = 0; i < Model.DueList.Count; i++)
{
@Html.HiddenFor(m => m.DueList[i].id)
@Html.DisplayFor(m => m.DueList[i].flat)
@Html.DisplayFor(m => m.DueList[i].name)
@Html.TextBoxFor(m => m.DueList[i].due)
}
}
Note also that the The next problem is that the model in the view is not typeof where Note also the addition of the Side note: Your are checking @Html.HiddenFor() helper need to be inside a element in order to be valid html.
List, but it does contain a property named DueList, which is typeof List so your POST method needs to bepublic ActionResult MakeDue(YourModel model)
YourModel is the class name you used to generate the view (i.e. in the @model ??? statement). Then you loop in the controller method need to beforeach (var BillId in model.DueList)
{
var getDue = db.BillChecks.Where(p => p.id == BillId.id).FirstOrDefault();
if (getDue != null) // add this
{
getDue.due = BillId.due;
}
}
db.SaveChanges();
if (getDue != null) check.if (ModelState.IsValid). It is recommended you return the view if ModelState is not valid so that the user can correct any errors.