Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 3 ASP.NET mvc中RadioButton控件的动态显示_Asp.net Mvc 3 - Fatal编程技术网

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

Asp.net mvc 3 ASP.NET mvc中RadioButton控件的动态显示,asp.net-mvc-3,Asp.net Mvc 3,我正在动态显示控件。我能够显示除单选按钮外的所有具有适当值的控件。单选按钮列表正在正确呈现,但未检查其值 foreach (var radio in item.RadioButtonList) { //Option 1 @Html.RadioButton(radio.Text, radio.Checked) @:  @radio.Text   //Option 2 @Html.Rad

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

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;
     }
foreach(项中的var无线电。无线电按钮列表)
{
//选择1
@Html.RadioButton(radio.Text,radio.Checked)@:@radio.Text
//选择2
@RadioButton(名称:radio.Text,值:radio.Checked,isChecked:radio.Checked)*@
//选择3
@:@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; }
 }
来自元素的公共类
{ 
公共列表RadioButtonList{get;set;}
}
公共类CBRBC控制
{
公共字符串文本{get;set;}
公共布尔检查{get;set;}
公共字符串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;

            }
public ActionResult FormDetails(int-formID)
{
if(form.Field\u Type\u Name==“单选按钮”)
{
radioList=新列表();
//对于选项1和2
放射科医生。添加(新CBRBControl
{
选中=正确,
Text=“无线电测试1”
});
放射科医生。添加(新CBRBControl
{
选中=错误,
Text=“无线电测试2”
});
//选择3
放射科医生。添加(新CBRBControl
{
IsRadioChecked=“已检查”,
Text=“无线电测试1”
});
放射科医生。添加(新CBRBControl
{
Text=“无线电测试2”
});
form.RadioButtonList=放射科医生;
}
我尝试使用there方法进行此操作。在选项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;  
<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;
<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
无线电测试2
对于选项2,生成的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;  
<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;
<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
无线电测试2
对于选项3,生成的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;  
<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;
<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
无线电测试2

您使用的是来自
单选按钮的错误重载。您还应该为此控件提供

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

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; }
}
@foreach (var radio in Model.RadioButtonList)
{
  @Html.RadioButton(radio.Name, radio.Value, radio.IsChecked) @:&nbsp; @radio.Text &nbsp;
}
并使用Html.RadioButton中的正确重载,如下所示:

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; }
}
@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;
无线电测试1
无线电测试2

您可以发布由帮助程序生成的HTML吗?很抱歉,我没有跟上。您想让我转到“查看源代码”并发布HTML吗?转到浏览器,使用页面检查器并发布生成的HTML!仅此单选按钮!这可能会有所帮助