Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.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

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 是否可以更改RadioButton的名称?_Asp.net_Asp.net Mvc 3_Radiobuttonfor - Fatal编程技术网

Asp.net 是否可以更改RadioButton的名称?

Asp.net 是否可以更改RadioButton的名称?,asp.net,asp.net-mvc-3,radiobuttonfor,Asp.net,Asp.net Mvc 3,Radiobuttonfor,我在视图中使用foreach循环来显示几个单选按钮行 sample radiobutton <tr> <td width="30%"> Integrity </td> <td width="17%">@Html.RadioButtonFor(x => x.main.ElementAt(i).nested.integrity, 1, new { id = "main_" + i + "__nested_inte

我在视图中使用foreach循环来显示几个单选按钮行

sample radiobutton

<tr>
    <td width="30%">
    Integrity
    </td>
    <td width="17%">@Html.RadioButtonFor(x => x.main.ElementAt(i).nested.integrity, 1, new { id = "main_" + i + "__nested_integrity) Poor
    </td>
    <td width="18%">@Html.RadioButtonFor(x => x.main.ElementAt(i).nested.integrity, 2, new { id = "main_" + i + "__nested_integrity" }) Satisfactory
     </td>
     <td width="18%">@Html.RadioButtonFor(x => x.main.ElementAt(i).nested.integrity, 3, new { id = "main_" + i + "__nested_integrity" }) Outstanding
     </td>
     <td width="16%">@Html.RadioButtonFor(x => x.main.ElementAt(i).nested.integrity, 4, new { id = "main_" + i + "__nested_integrity" }) Off
     </td>
     </tr>
正如您所看到的,所有循环的名称属性都相同,但id不同。
现在我的问题是……是否有可能覆盖RadioButton的name属性作为like id??

现在我的问题是…是否有可能覆盖RadioButton的name属性,比如id

不,那是不可能的。name属性将根据作为第一个参数传递的lambda表达式计算

就我个人而言,我会使用编辑器模板,根本不需要任何循环

@model MainViewModel
<table>
    <thead>
        ...
    </thead>
    <tbody>
        @Html.EditorFor(x => x.main)
    </tbody>
</table>
@model MainViewModel
...
@EditorFor(x=>x.main)
在相应的编辑器模板中:

@model ChildViewModel
<tr>
    <td width="30%">
        Integrity
    </td>
    <td width="17%">
        @Html.RadioButtonFor(x => x.nested.integrity, 1) Poor
    </td>
    <td width="18%">
        @Html.RadioButtonFor(x => x.nested.integrity, 2) Satisfactory
     </td>
     <td width="18%">
         @Html.RadioButtonFor(x => x.nested.integrity, 3) Outstanding
     </td>
     <td width="16%">
         @Html.RadioButtonFor(x => x.nested.integrity, 4) Off
     </td>
</tr>
@model儿童视图模型
诚实正直
@RadioButton(x=>x.nested.integrity,1)较差
@Html.radioButton(x=>x.nested.integrity,2)令人满意
@RadioButton(x=>x.nested.integrity,3)优秀
@RadioButton(x=>x.nested.integrity,4)关闭

是,使用编辑器模板。我已经更新了我的答案来展示一个例子。这就是我在答案中展示的自定义编辑器模板中发生的事情。确保已遵守编辑器模板的命名约定。例如,如果主视图模型上的
main
属性的类型为
IEnumerable
,则编辑器模板文件必须位于
~/Views/Shared/EditorTemplates/ChildViewModel.cshtml
中。请注意模板的位置和名称。如果您不遵守约定,将使用默认模板,该模板显然不会生成单选按钮。对不起,darin,这次我不了解您,请您详细说明一下。我有一个主类,其中ICollection嵌套为其导航属性,然后再次将NestedOverested嵌套为包含radiobutton登录名的嵌套导航属性…现在,如果可能,请为我澄清OK,因此,如果
main
属性的类型为
ICollection
,那么编辑器模板必须是
~/Views/Shared/EditorTemplates/nested.cshtml
。还有一个问题。。是否有必要在编辑器模板本身中包含所有需要的纸条和css文件?和视图中的小脚本
@model ChildViewModel
<tr>
    <td width="30%">
        Integrity
    </td>
    <td width="17%">
        @Html.RadioButtonFor(x => x.nested.integrity, 1) Poor
    </td>
    <td width="18%">
        @Html.RadioButtonFor(x => x.nested.integrity, 2) Satisfactory
     </td>
     <td width="18%">
         @Html.RadioButtonFor(x => x.nested.integrity, 3) Outstanding
     </td>
     <td width="16%">
         @Html.RadioButtonFor(x => x.nested.integrity, 4) Off
     </td>
</tr>