在嵌套for循环中使用RadioButtonFor和CheckBoxFor(razor语法)

时间:2016-07-19 20:08:58

标签: asp.net-mvc razor

我的代码如下所示:

@for (var i = 0; i < Model.Count; i++)
{
    <h2>Question @Html.DisplayFor(x => x[i].OrderIndex) </h2>
    @Html.HiddenFor(x => x[i].SurveyId)
    @Html.HiddenFor(x => x[i].QuestionId)
    @Html.HiddenFor(x => x[i].AnswerId)
    <br />
    @Html.DisplayFor(x => x[i].QuestionText)
    <br />
    @Html.DisplayFor(x => x[i].QuestionText)
    @if (Model[i].Type == QuestionType.Single)
    {
        <h2>Answer</h2>
        @for (var ao = 0; ao < Model[i].Answer.AnswerOptions.Count; ao++)
        {
            @Html.RadioButtonFor(x => x[i].Answer.AnswerOptions[ao].IsSelectedAnswer, true) @Html.Raw("&nbsp;");
            @Html.LabelFor(x => x[i].Answer.AnswerOptions[ao].IsSelectedAnswer, Model[i].Answer.AnswerOptions[ao].OptionText)<br />
            @Model[i].Answer.AnswerOptions[ao].OptionText
        }
    }
}

我可以让所有其他控件类型正常工作,但我不知道如何将单选按钮连接到我的模型。它是嵌套的for循环。

我也试过了:

@Html.RadioButtonFor(x => x[i].Answer.AnswerOptions[ao].IsSelectedAnswer, Model[i].Answer.AnswerOptions[ao].OptionText) 
@Html.Raw("&nbsp;");

这应该得到价值,但事实并非如此。此外,我无法获得displayfor或labelfor working:

@Html.LabelFor(x => x[i].Answer.AnswerOptions[ao].IsSelectedAnswer, Model[i].Answer.AnswerOptions[ao].OptionText)
<br />

0 个答案:

没有答案