用于动态调查表的MVC模型

时间:2014-02-21 09:22:47

标签: asp.net-mvc model

考虑一个动态调查表,其中包含 n个问题,对于每个问题,可以有 n个答案

<form>
<!-- Question block -->
    <input id="QuestionText" name="QuestionText" placeholder="Text otázky" value="" type="text">
    <table>
        <!-- Answer block -->
        <tr><td><input name="AnswerIsCorrect" type="checkbox"></td>
            <td><input name="AnswerText" type="text"></td>
        </tr>
        <!-- Answer block END -->
        <tr><td><input name="AnswerIsCorrect" type="checkbox"></td>
            <td><input name="AnswerText" type="text"></td>
        </tr>
        <!-- More answers -->
    </table>
    <input id="QuestionComment" type="text">
    <!-- Question block END -->

    <!-- More questions -->
</form>

是否有可能让MVC将提交解析为类似的结构:

public  class CreateSurveyModel
{
    public List<QuestionModel> Questions { get; set; }
}
public class QuestionModel
{
    public string QuestionText { get; set; }
    public string QuestionComment { get; set; }
    public List<AnswerModel> Answers { get; set; }
}
public class AnswerModel
{
    public string AnswerText { get; set; }
    public bool IsCorrect { get; set; }
}

如果是这样的话?

编辑(如答案中所示):

@using(Html.BeginForm("Send", "Try", FormMethod.Post/*or FormMethod.Get*/))
{
    foreach(var question in Model.Questions)
    {
        <!-- Question block -->

         @Html.TextBox("QuestionText", question.QuestionText)            
         <table>
             @foreach(var answer in question.Answers)
             {
                 <!-- Answer block -->
                 <tr>
                     <td>@Html.CheckBox("AnswerIsCorrect", answer.IsCorrect)</td>
                     <td>@Html.TextBox("AnswerText", answer.AnswerText )</td>
                 </tr>
                 <!-- Answer block END -->
             }
         </table>
         @Html.TextBox("QuestionComment", question.QuestionComment)
         <!-- Question block END -->
    }
    <input type="submit"/>
}

行动:

[HttpPost]
public ActionResult Send(CreateSurveyModel model)
{
    return Index();
}

但是model.Questions为null

2 个答案:

答案 0 :(得分:1)

在视图中使用以下结构:

@using (Html.BeginForm())
{
    for (int i = 0; i < Model.Questions.Count(); i++)
    {
        @Html.TextBoxFor(model => model.Questions[i].QuestionText, new { placeholder = "Text otázky" })
        <table>
            @for (int j = 0; j < Model.Questions[i].Answers.Count(); j++)
            {
                <!-- Answer block -->
                <tr>
                    <td>@Html.CheckBoxFor(model => model.Questions[i].Answers[j].IsCorrect)</td>
                    <td>@Html.TextBoxFor(model => model.Questions[i].Answers[j].AnswerText)</td>
                </tr>
            }
        </table>
        @Html.TextBoxFor(model => model.Questions[i].QuestionComment)
        <!-- Question block END -->
    }
    <input type="submit"/>
}

此代码可帮助您正确填充视图,并将模型正确传递给视图。

<强>更新

您可以使用以下操作方法进行测试

public ActionResult Send()
{
    CreateSurveyModel model = new CreateSurveyModel();
    model.Questions = new List<QuestionModel>()
    {
        new QuestionModel()
        {
            QuestionText = "1",
            QuestionComment = "Comment 1",
            Answers = new List<AnswerModel>()
            {
                new AnswerModel()
                {
                    AnswerText = "A1",
                    IsCorrect = false,
                },
                new AnswerModel()
                {
                    AnswerText = "A2",
                    IsCorrect = true,
                },
                new AnswerModel()
                {
                    AnswerText = "A3",
                    IsCorrect = false,
                },
                new AnswerModel()
                {
                    AnswerText = "A4",
                    IsCorrect = true,
                },
            }
        },
        new QuestionModel()
        {
            QuestionText = "2",
            QuestionComment = "Comment 2",
            Answers = new List<AnswerModel>()
            {
                new AnswerModel()
                {
                    AnswerText = "A5",
                    IsCorrect = false,
                },
                new AnswerModel()
                {
                    AnswerText = "A6",
                    IsCorrect = false,
                },
                new AnswerModel()
                {
                    AnswerText = "A7",
                    IsCorrect = false,
                },
                new AnswerModel()
                {
                    AnswerText = "A8",
                    IsCorrect = true,
                },
                new AnswerModel()
                {
                    AnswerText = "A9",
                    IsCorrect = false,
                },
            }
        }
    };

    return View(model);
}

[HttpPost]
public ActionResult Send(CreateSurveyModel model)
{
    return View();
}

注意:不要忘记您应该填写一些数据供您查看。

答案 1 :(得分:0)

你可以这样做:

@using(Html.BeginForm("action", "controller", FormMethod.Post/*or FormMethod.Get*/))
{
    foreach(var question in Model.Questions)
    {
         <!-- Question block -->
         @Html.TextBoxFor("QuestionText", question.QuestionText)            
         <table>
             @foreach(var answer in question.Answers)
             {
                 <!-- Answer block -->
                 <tr>
                     <td>@Html.Checkbox("AnswerIsCorrect", answer.IsCorrect)</td>
                     <td>@Html.TextBox("AnswerText", answer.AnswerText )</td>
                 </tr>
                 <!-- Answer block END -->
             }
         </table>
         @Html.TextBox("QuestionComment", question.QuestionComment)
         <!-- Question block END -->
    }
}
相关问题