1
我有這樣的模式:對象未添加到收藏
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>
}
任何人可以幫助我嗎?