1

我有一些用于显示从数据库驱动的问卷的嵌套视图模型。显示问题很简单,但我不知道如何绑定到模型或解析提交时选择的答案。

我的对象看起来像(伪):

public class QuestionSheetViewModel(){
    public list<QuestionViewModel> Questions;
    public string UserName;
}

public class QuestionViewModel(){
    public int QuestionId;
    public string QuestionText;
    public dictionary<int, string> answers;
}

在我看来,我遍历问题,显示每个问题文本,然后将每个答案呈现为单选按钮:

    @Html.RadioButton(question.QuestionID, answer.Key());

通过使用 question.questionId 作为单选按钮名称,它们被组合在一起,将用户限制为每个问题一个答案。

当我提交时,模型的问题列表是空的。是否可以自动将其绑定到我的视图模型?如果没有,我需要做什么来解析提交的表单以找到每个问题的选定答案?

谢谢

4

1 回答 1

2

您可以稍微调整您的模型以符合您的要求:

public class QuestionSheetViewModel
{
    public List<QuestionViewModel> Questions { get; set; }
    public int QuestionId { get; set; }
}

public class QuestionViewModel
{
    public string QuestionText { get; set; }
    public int SelectedAnswerId { get; set; }
    public List<AnswerViewModel> Answers { get; set; }
}

public class AnswerViewModel
{
    public int AnswerId { get; set; }
    public string Text { get; set; }
}

然后有一个控制器:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new QuestionSheetViewModel
        {
            QuestionId = 1,
            Questions = new[] 
            { 
                new QuestionViewModel
                {
                    QuestionText = "question 1",
                    SelectedAnswerId = 2,
                    Answers = new[] 
                    {
                        new AnswerViewModel { AnswerId = 1, Text = "answer 1" },
                        new AnswerViewModel { AnswerId = 2, Text = "answer 2" },
                    }.ToList()
                },
                new QuestionViewModel
                {
                    QuestionText = "question 2",
                    SelectedAnswerId = 3,
                    Answers = new[] 
                    {
                        new AnswerViewModel { AnswerId = 3, Text = "answer 3" },
                        new AnswerViewModel { AnswerId = 4, Text = "answer 4" },
                    }.ToList()
                },
            }.ToList()
        };
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(QuestionSheetViewModel model)
    {
        // When the form is submitted the model will be properly bound
        return View(model);
    }
}

带有相应的视图 ( ~/Views/Home/Index.cshtml):

@model QuestionSheetViewModel

@using (Html.BeginForm())
{ 
    @Html.HiddenFor(x => x.QuestionId)
    <ul>
        @Html.EditorFor(x => x.Questions)
    </ul>
    <button type="submit">OK</button>
}

以及一个相应的编辑器模板,它将按照惯例为每个问题自动呈现 ( ~/Views/Shared/EditorTemplates/QuestionViewModel.cshtml):

@model QuestionViewModel

<li>
    @Html.DisplayFor(x => x.QuestionText)
    @Html.HiddenFor(x => x.QuestionText)
    <ul>
        @for (int i = 0; i < Model.Answers.Count; i++)
        {
            <li>
                @Html.HiddenFor(x => x.Answers[i].AnswerId)
                @Html.HiddenFor(x => x.Answers[i].Text)
                @Html.RadioButtonFor(x => x.SelectedAnswerId, Model.Answers[i].AnswerId)
                @Html.DisplayFor(x => x.Answers[i].Text)
            </li>
        }
    </ul>
</li>
于 2013-04-03T16:00:55.837 回答