ASP.NET mvc中RadioButton控件的动态显示

时间:2013-08-08 18:28:00

标签: asp.net-mvc-3

我正在动态显示控件。我可以使用适当的值显示所有控件,但单选按钮除外。单选按钮列表正确呈现,但未检查其值。

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;

1 个答案:

答案 0 :(得分: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;