У меня есть частичное представление для редактирования коллекции (большинство из которых я здесь усек):
@using CustomSurveyTool.Models
@model Basiclife
<div class="editorRow">
@using (Html.BeginCollectionItem(""))
{
<div class="form-horizontal">
<div class="row">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="col-md-2">
@Html.LabelFor(model => model.Plantype, htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.Plantype, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Plantype, "", new { @class = "text-danger" })
</div>
<div class="col-md-1">
<a href="#" class="deleteRow">X</a>
</div>
</div>
</div>
}
</div>
Что является частью представления оболочки:
@using CustomSurveyTool.Models
@model IEnumerable<Basiclife>
@{
ViewBag.Title = "CreateBasicLifeResponse";
}
<h2>Basic Life Insurance Plans</h2>
<div id="planList">
@using (Html.BeginForm())
{
<div id="editorRows">
@foreach (var item in Model)
{
@Html.Partial("_BasicLifeResponse", item)
}
</div>
@Html.ActionLink("Add", "BasicLifeResponse", null, new { id = "addItem", @class = "button" });
<input type="submit" value="submit" />
}
</div>
Какиеотправляет обратно на этот контроллер:
[HttpPost]
public ActionResult CreateBasicLifeResponse(IEnumerable<Basiclife> model)
{
foreach(var item in model)
{
string currentUserId = User.Identity.GetUserId();
Response targetresponse = db.response.FirstOrDefault(x => x.Userid == currentUserId);
int responseid = targetresponse.Id;
item.ResponseId = responseid;
db.basiclife.Add(item);
db.SaveChanges();
}
List<Basiclife> basiclife = new List<Basiclife>();
return View(model);
}
Я получаю исключение NullReferenceException для foreach(var item in model)
после отправки формы.Что может быть причиной этого?