0

我正在动态显示控件。除了单选按钮之外,我能够显示具有适当值的所有控件。单选按钮列表正确呈现,但未检查其值。

foreach (var radio in item.RadioButtonList)
    {
        //Option 1

        @Html.RadioButton(radio.Text, radio.Checked) @:  @radio.Text  

        //Option 2

        @Html.RadioButton(name: radio.Text, value: radio.Checked, isChecked: radio.Checked)*@

        //Option 3

        <input type="radio" value="radio.Text"  checked="@radio.Checked"/> @:&nbsp; @radio.Text &nbsp;
     }

我的模型是

 public class FromElement
 { 
     public List<CBRBControl> RadioButtonList { get; set; }
 }

 public class CBRBControl
 {
     public string Text { get; set; }

     public bool Checked { get; set; }

     public string IsRadioChecked { get; set; }
 }

我的控制器

 public ActionResult FormDetails(int formID)
 {
            if (form.Field_Type_Name == "Radio Button")
            {
                radioList = new List<CBRBControl>();

                // For option 1 and 2

                radioList.Add(new CBRBControl
                {
                    Checked = true,
                    Text = "Radio Test 1"
                });

                radioList.Add(new CBRBControl
                {
                    Checked = false,
                    Text = "Radio Test 2"
                });

                // For option 3

                radioList.Add(new CBRBControl
                {
                    IsRadioChecked = "checked",
                    Text = "Radio Test 1"
                });

                radioList.Add(new CBRBControl
                {
                    Text = "Radio Test 2"
                });

                form.RadioButtonList = radioList;

            }

我尝试使用那里的方法来解决这个问题。在选项 1 和 2 中,列表中的任何单选按钮都没有被选中,而在选项 3 中,所有单选按钮都被选中。

对于选项 1,生成的 html 是

<input id="Radio_Test_1" name="Radio Test 1" type="radio" value="True"> &nbsp; Radio Test 1 &nbsp;
<input id="Radio_Test_2" name="Radio Test 2" type="radio" value="False"> &nbsp; Radio Test 2 &nbsp;  

对于选项 2,生成的 html 是

<input checked="checked" id="Radio_Test_1" name="Radio Test 1" type="radio" value="True"> &nbsp; Radio Test 1 &nbsp;
<input id="Radio_Test_2" name="Radio Test 2" type="radio" value="False"> &nbsp; Radio Test 2 &nbsp;

对于选项 3,生成的 html 是

<input type="radio" value="radio.Text" checked="checked"> &nbsp; Radio Test 1 &nbsp;
<input type="radio" value="radio.Text" checked=""> &nbsp; Radio Test 2 &nbsp;
4

1 回答 1

0

您使用了错误的重载RadioButton。您还应该Value为此控件提供 。

将您的模型更改为以下内容:

public class CBRBControl
{
    //
    // Name should be unique, given that the HTML.RaadioButton,
    // will use this to generate the input attribute id, that
    // must be unique in the sabe HTML document
    //
    public string Name { get; set; }
    public string Text { get; set; } 
    public string Value { get; set; }
    public bool IsChecked { get; set; }
}

并使用 Html.RadioButton 中的正确重载,如下所示:

@foreach (var radio in Model.RadioButtonList)
{
  @Html.RadioButton(radio.Name, radio.Value, radio.IsChecked) @:&nbsp; @radio.Text &nbsp;
}

生成的 HTML 将如下所示:

<input id="radio_0" name="radio_0" type="radio" value="1" /> &nbsp; Radio Test 1 &nbsp;
<input checked="checked" id="radio_1" name="radio_1" type="radio" value="2" /> &nbsp; Radio Test 2 &nbsp;
于 2013-08-08T19:51:55.247 回答