I have converted the edit function of a generated controller entity framework with views of my model. Instead of the edit handling one instance of my model at a time I send a list of all of my instances and loop through them in a Html.BeginForm
in my razor view. When I post the form I try to pass the list as a parameter into my controller. I want a list of all the instances with all their the updated values however the list is posted as null. I cannot see that it is a markup issue. Please help.
Here is my controller:
public ActionResult Allotment(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
List<ClaimFile> claimFiles = db.ClaimFiles.Where(b => b.ClaimID == id).ToList();
if (claimFiles.Count == 0)
{
return HttpNotFound();
}
ViewBag.DocumentListID = new SelectList(db.DocumentLists, "DocumentListID", "Description", claimFiles.FirstOrDefault().DocumentListID);
return View(claimFiles);
}
View:
@model List<Harry.ClaimFile>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<h4>ClaimFile</h4>
<hr />
@foreach (var claimfile in Model)
{
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => claimfile.ClaimFileID)
<div class="form-group">
@Html.LabelFor(model => claimfile.DocumentListID, "DocumentListID", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("DocumentListID", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => claimfile.DocumentListID, "", new { @class = "text-danger" })
</div>
</div>
}
<input type="submit" value="Save" class="btn btn-default" />
}
Then here on the post controller I receive null when I hit the save button on my razor view:
[HttpPost]
public ActionResult Allotment(List<ClaimFile> claimFiles)
{
//List<ClaimFile> claimFiles = null. This is my problem
}
Please show me what I have done wrong or give a solution to this problem. Thanks.