我有这个型号:
public class QuestionDTO
{
public Question Question { get; set; }
public OpenQuestionAnwser OpenQuestionAnwser { get; set; }
public List<ChoiceQuestionAnwser> ChoiceQuestionAnwsers { get; set; }
public void AddChoiceQuestionAnwser()
{
ChoiceQuestionAnwsers.Add(new ChoiceQuestionAnwser() {ChoiceQuestionAnwserID = ChoiceQuestionAnwsers.Count});
}
public void RemoveChoiceQuestionAnwser(int index)
{
ChoiceQuestionAnwsers.RemoveAt(index);
}
public void Save()
{
}
}
我想创建一个ajax表单来动态地将对象添加到集合中,但是我有问题,因为当我将表单发送到服务器时,我得到一个空集合。我尝试了几种不同的方法,但没有运气。
表格代码:
@using (Ajax.BeginForm("AddChoiceQuestionAnwser","Question",new AjaxOptions
{
UpdateTargetId = "chooseAnwsersTableBody"
}))
{
<table class="hovered" id="anwsers">
<tr>
<th>Poprawna</th>
<th>Treść odpowiedzi</th>
<th></th>
</tr>
<tbody id="chooseAnwsersTableBody">
@Html.Partial("ChooseAnwsersView",Model)
</tbody>
</table>
@Html.HiddenFor(a => a.ChoiceQuestionAnwsers);
@Html.HiddenFor(a => a.OpenQuestionAnwser)
@Html.HiddenFor(a => a.Question)
<div>
<input type="submit" value="Dodaj odpowiedź"/>
</div>
}
和控制器代码:
[HttpPost]
public ActionResult AddChoiceQuestionAnwser(QuestionDTO questionDto)
{
questionDto.AddChoiceQuestionAnwser();
return PartialView("ChooseAnwsersView", questionDto);
}
和部分视图
@model BusinessLogic.DTO.Test.QuestionDTO
@{
ViewBag.Title = "ChooseAnwsersView";
}
@foreach(var item in Model.ChoiceQuestionAnwsers)
{
<tr>
<td> @Html.CheckBox("ChoiceQuestionAnwsers["+item.ChoiceQuestionAnwserID+"].IsCorrect",item.IsCorrect,new {@id="ChoiceQuestionAnwsers_"+item.ChoiceQuestionAnwserID+"__IsCorrect"})
@Html.Hidden("ChoiceQuestionAnwsers[" + item.ChoiceQuestionAnwserID + "].ChoiceAnwserType", item.ChoiceAnwserType, new { @id = "ChoiceQuestionAnwsers_" + item.ChoiceQuestionAnwserID + "__ChoiceAnwserType" })
@Html.Hidden("ChoiceQuestionAnwsers[" + item.ChoiceQuestionAnwserID + "].ChoiceQuestionAnwserID", item.ChoiceQuestionAnwserID, new { @id = "ChoiceQuestionAnwsers_" + item.ChoiceQuestionAnwserID + "__ChoiceQuestionAnwserID" })
@Html.Hidden("ChoiceQuestionAnwsers[" + item.ChoiceQuestionAnwserID + "].Data", item.Data, new { @id = "ChoiceQuestionAnwsers_" + item.ChoiceQuestionAnwserID + "__Data" })
@Html.Hidden("ChoiceQuestionAnwsers[" + item.ChoiceQuestionAnwserID + "].Question", item.Question, new { @id = "ChoiceQuestionAnwsers_" + item.ChoiceQuestionAnwserID + "__Question" })
@Html.Hidden("ChoiceQuestionAnwsers[" + item.ChoiceQuestionAnwserID + "].QuestionID", item.QuestionID, new { @id = "ChoiceQuestionAnwsers_" + item.ChoiceQuestionAnwserID + "__QuestionID" })
</td>
<td>@Html.Editor("ChoiceQuestionAnwsers[" + item.ChoiceQuestionAnwserID + "].Text", item.Text, new { @id = "ChoiceQuestionAnwsers_" + item.ChoiceQuestionAnwserID + "__Text" })
</td>
<td><a href='#' data-bind='click: $root.removeGift'>Usuń</a></td>
</tr>
}
有人能帮助我吗?
答案 0 :(得分:0)
尝试强力输入表单:
@for (int i = 0; i < Model.ChoiceQuestionAnwsers.Count; i++)
{
@Html.CheckBoxFor(m => m.ChoiceQuestionAnwsers[i].IsCorrect)
...
}
在局部视图中。