我正在动态显示控件。除了单选按钮之外,我能够显示具有适当值的所有控件。单选按钮列表正确呈现,但未检查其值。
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"/> @: @radio.Text
}
我的模型是
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"> Radio Test 1
<input id="Radio_Test_2" name="Radio Test 2" type="radio" value="False"> Radio Test 2
对于选项 2,生成的 html 是
<input checked="checked" id="Radio_Test_1" name="Radio Test 1" type="radio" value="True"> Radio Test 1
<input id="Radio_Test_2" name="Radio Test 2" type="radio" value="False"> Radio Test 2
对于选项 3,生成的 html 是
<input type="radio" value="radio.Text" checked="checked"> Radio Test 1
<input type="radio" value="radio.Text" checked=""> Radio Test 2