Я добавил дополнительные три поля ввода для моего представления, чтобы позволить системному администратору одновременно отправлять четыре объекта вместо одного объекта за раз; вид выглядит следующим образом: -Не удалось отправить список объектов привязке модели с помощью ICollection
@model Elearning.Models.Answer
@{
ViewBag.Title = "Create";
}
<div id = "partialWrapper">
@using (Ajax.BeginForm("Create", "Answer", new AjaxOptions
{
HttpMethod = "Post",
InsertionMode = InsertionMode.InsertAfter,
UpdateTargetId = "incrementanswer",
OnSuccess = "removePartial",
LoadingElementId = "progress2"
}))
{
<div id = "returnedquestion">
@Html.ValidationSummary(true)
<fieldset>
<legend>Answer here</legend>
<ol>
<li> <div class="editor-label">
@Html.LabelFor(model => model.Description)
</div>
<div class="editor-field">
@Html.TextBox("answer[0].Description")
@Html.ValidationMessageFor(model => model.Description)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.IsRight)
</div>
<div class="editor-field">
@Html.DropDownList("IsRight", String.Empty)
@Html.ValidationMessageFor(model => model.IsRight)
</div>
</li>
<li> <div class="editor-label">
@Html.LabelFor(model => model.Description)
</div>
<div class="editor-field">
@Html.TextBox("answer[1].Description")
@Html.ValidationMessageFor(model => model.Description)
</div> <div class="editor-label">
@Html.LabelFor(model => model.IsRight)
</div>
<div class="editor-field">
@Html.DropDownList("IsRight", String.Empty)
@Html.ValidationMessageFor(model => model.IsRight)
</div> </li>
<li> <div class="editor-label">
@Html.LabelFor(model => model.Description)
</div>
<div class="editor-field">
@Html.TextBox("answer[2].Description")
@Html.ValidationMessageFor(model => model.Description)
</div> <div class="editor-label">
@Html.LabelFor(model => model.IsRight)
</div>
<div class="editor-field">
@Html.DropDownList("IsRight", String.Empty)
@Html.ValidationMessageFor(model => model.IsRight)
</div> </li>
<li> <div class="editor-label">
@Html.LabelFor(model => model.Description)
</div>
<div class="editor-field">
@Html.TextBox("answer[3].Description")
@Html.ValidationMessageFor(model => model.Description)
</div> <div class="editor-label">
@Html.LabelFor(model => model.IsRight)
</div>
<div class="editor-field">
@Html.DropDownList("IsRight", String.Empty)
@Html.ValidationMessageFor(model => model.IsRight)
</div> </li>
<ol>
</fieldset>
<input type= "hidden" name = "questionid" value = @ViewBag.questionid>
<input type= "hidden" name = "assessmentid" value = @ViewBag.assessmentid>
<input type="submit" value="Add answer" />
</div>
}
</div>
и следующее сообщение Ation Метод: -
[HttpPost]
public ActionResult Create(int questionid, ICollection<Answer> answer)
{
if (ModelState.IsValid)
{
foreach (var a in answer){
repository.AddAnswer(a);
repository.Save();
}
return PartialView("_details2",answer);
}
return View("_details2",answer);}
и последнее, что частичный вид _details2, который содержит новые добавленные объекты: -
@model IEnumerable<Elearning.Models.Answer>
@{
ViewBag.Title = "Details";
}
@foreach (var m in Model)
{
<tr id = @m.AnswersID>
<td>
@Html.DisplayFor(modelItem => m.Description)
</td>
<td>
@*@Html.DisplayFor(modelItem => Model.Answer_Description.description)*@
@ViewBag.Answerdesription
</td>
<td>
@Ajax.ActionLink("Delete", "Delete", "Answer",
new { id = m.AnswersID },
new AjaxOptions
{
Confirm = "Are You sure You want to delete this Answer ?",
HttpMethod = "Post",
UpdateTargetId = @m.AnswersID.ToString(),
OnSuccess = "removePartial2"
})
</td>
</tr>
}
но выше не работает nethier объекты будут добавлены или частичный вид будет возвращен, так как я могу решить эту проблему ??? BR