radiobuttonfor grouping razor mvc5

时间:2014-01-06 01:34:54

标签: asp.net-mvc razor

当我对单选按钮进行分组时,所选的值不会被回发,

所以,使用这个,SelectedQuestionId 空白

<div class="row">
        <div class="col-md-4">
            <div class="form-group">

                @Html.HiddenFor(x => x.UserResponses[0].QuestionId)
                @Html.RadioButtonFor(x => x.UserResponses[0].SelectedQuestionId, Model.UserResponses[0].QuestionId, new { Name ="selectone"})
                @Html.DisplayTextFor(x => x.UserResponses[0].QuestionText)
                @Html.HiddenFor(x => x.UserResponses[0].QuestionText)
                @Html.HiddenFor(x => x.UserResponses[0].InputType)
                @Html.HiddenFor(x => x.UserResponses[0].Points)

            </div>
        </div>

        <div class="col-md-4">
            <div class="form-group">
                <span class="text-muted">@Html.DisplayTextFor(x => x.UserResponses[0].Points)</span>&nbsp;@Html.LabelFor(x => x.UserResponses[0].Points, new { @class = "text-muted" })
            </div>
        </div>

    </div>

    <div class="row">
            <div class="col-md-4">
                <div class="form-group">

                    @Html.HiddenFor(x => x.UserResponses[1].QuestionId)
                    @Html.RadioButtonFor(x => x.UserResponses[1].SelectedQuestionId, Model.UserResponses[1].QuestionId, new { Name = "selectone"})
                    @Html.DisplayTextFor(x => x.UserResponses[1].QuestionText)
                    @Html.HiddenFor(x => x.UserResponses[1].QuestionText)
                    @Html.HiddenFor(x => x.UserResponses[1].InputType)
                    @Html.HiddenFor(x => x.UserResponses[1].Points)




                </div>
            </div>

            <div class="col-md-4">
                <div class="form-group">
                    <span class="text-muted">@Html.DisplayTextFor(x => x.UserResponses[1].Points)</span>&nbsp;@Html.LabelFor(x => x.UserResponses[1].Points, new { @class = "text-muted" })
                </div>
            </div>

        </div>

但是,当我从radiobutton中删除分组时,一切运行良好,并且SelectedQuestionId填充了questionid。因此,用这些替换上面的RadioButtonFor允许在发布表单时填充SelectedQuestionId。 上面的代码基于此question,虽然我已经提出了相关问题here - 但这个问题完全是分开的。

@Html.RadioButtonFor(x => x.UserResponses[0].SelectedQuestionId, Model.UserResponses[0].QuestionId)

@Html.RadioButtonFor(x => x.UserResponses[1].SelectedQuestionId, Model.UserResponses[1].QuestionId)

1 个答案:

答案 0 :(得分:0)

找到了解决这个相当棘手的问题的方法。

@Html.RadioButtonFor(x => x.UserResponses[0].SelectedQuestionId, Model.UserResponses[0].QuestionId)

@Html.RadioButtonFor(x => x.UserResponses[1].SelectedQuestionId, Model.UserResponses[1].QuestionId)

每个单选按钮上面的代码在反序列化过程中都有一个唯一的名称,如果按照预期选择了给定项目,则会为给定项目填充SelectedQuestionId属性。但是,对于属于一个组的无线电按钮,它们需要具有相同的名称属性,并且由于在上述情况中不是这种情况,因此可以选择两个单选按钮。这也意味着选择了多个单选按钮,所有选定的单选按钮的属性将填充其各自的QuestionId。

为了解决这个问题,总是可以编写代码,

@Html.RadioButtonFor(x => x.UserResponses[0].SelectedQuestionId, Model.UserResponses[0].QuestionId, new { Name = "selectone"})
@Html.RadioButtonFor(x => x.UserResponses[1].SelectedQuestionId, Model.UserResponses[1].QuestionId, new { Name = "selectone"})

而且,现在只能选择一个单选按钮,但是在HttpPost方法的反序列化过程中,集合中每个项目的SelectedQuestionId属性将为null。显然,radiobuttons的名称现在是“selectone”,因此在SelectedQuestionId的键/值配对中,该值不会被填充,换句话说,即使对于所选的单选按钮,SelectedQuestionId也总是为空。

所以,解决方案如下:

 @Html.RadioButtonFor(x => x.SelectedId, Model.UserResponses[i].QuestionId)

x.SelectedId位于ParentModel中,并且填充了所选问题的QuestionId,并且由于两个单选按钮共享相同的名称,因此它们属于同一组,因此只有一个单选按钮可以是地选择。

相关问题