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>
}

有谁能够帮我?

4

1 回答 1

0

尝试强输入表单:

@for (int i = 0; i < Model.ChoiceQuestionAnwsers.Count; i++) 
{
    @Html.CheckBoxFor(m => m.ChoiceQuestionAnwsers[i].IsCorrect)
    ...
}

在你的局部视图里面。

于 2013-02-15T23:21:15.673 回答