0

当用户选择单选按钮时,IsSelected属性仍在false回发,为什么?

看法

在此处输入图像描述

@using (Html.BeginForm())
{
    <fieldset>
        <legend>UserChoice</legend>

        @Html.DisplayNameFor(model => model.IsSelected)
        @Html.TextBoxFor(model => model.FirstName)

        @Html.RadioButtonFor(uc => uc.IsSelected, false)

        <input type="submit" name="name" value="Submit Form" />
    </fieldset>
}

控制器

在此处输入图像描述

4

4 回答 4

1

如果您有不同类型的选项,您可以使用以下

一个非常简单的实现方法是这样的

@Html.RadioButtonFor(modelItem =>item.SelectedOption, "Option1")
@Html.RadioButtonFor(modelItem =>item.SelectedOption, "Option2")
@Html.RadioButtonFor(modelItem =>item.SelectedOption, "Option3")

或者你也可以使用,

@Html.RadioButtonFor(modelItem =>item.SelectedOption, item.Option1)
@Html.RadioButtonFor(modelItem =>item.SelectedOption, item.Option2)
@Html.RadioButtonFor(modelItem =>item.SelectedOption, item.Option3)

如果你只是想要isSelected或者true使用false这个。

@Html.RadioButtonFor(uc => uc.IsSelected, true)
于 2013-08-27T05:24:21.017 回答
0

not sure if that your issue but if it checked than you expect to get false try makink the value true

 @Html.RadioButtonFor(uc => uc.IsSelected, true)
于 2013-08-27T04:53:38.023 回答
0

The overload that you are using binds the value of the radio input element to the provided value - so you are generating markup like this:

<input type="radio" name="isSelected" value="false">

When this gets submitted back to your controller .NET deserializes that value to a boolean - which in this case, is false.

To fix it, use true as the second argument:

@Html.RadioButtonFor(uc => uc.IsSelected, true)
于 2013-08-27T04:55:36.840 回答
0

尝试这个,

@Html.RadioButtonFor(uc => uc.IsSelected, true)

或者

 @Html.RadioButton("IsSelected", true)
于 2013-08-27T05:10:18.187 回答