可能改变RadioButtonFor的名字吗? [英] Possible to change name of RadioButtonFor?

查看:181
本文介绍了可能改变RadioButtonFor的名字吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用foreach循环insoide我的视图中显示几行单选框..

 样品单选按钮&所述; TR>
    &所述; TD宽度=30%>
    廉正
    < / TD>
    < TD WIDTH =17%> @ Html.RadioButtonFor(X => x.main.ElementAt(I).nested.integrity,1,新{ID =main_+ I +__nested_integrity)差评
    < / TD>
    < TD WIDTH =18%> @ Html.RadioButtonFor(X => x.main.ElementAt(I).nested.integrity,2,新的{ID =main_+ I +__nested_integrity})满意的
     < / TD>
     < TD WIDTH =18%> @ Html.RadioButtonFor(X => x.main.ElementAt(I).nested.integrity,3,新{ID =main_+ I +__nested_integrity})优秀
     < / TD>
     < TD WIDTH =16%> @ Html.RadioButtonFor(X => x.main.ElementAt(I).nested.integrity,4,新{ID =main_+ I +__nested_integrity})离
     < / TD>
     < / TR>




因为我得到的问题,同时示范因此结合我创建手动ID,以满足我的要求(不同的递增ID)。

但同样的问题带有name属性,我认为。
对于第一次和每一个循环我得到相同的名称属性(不加),即如果我从第一循环选择radiobuttton然后从其他循环取消taht一行。




 循环1 ID =main_0__nested_integrity
循环2的id =main_0__nested_integrity
循环1名=nested.integrity
循环2名=nested.integrity

你可以看到name属性所有环路都一样,用不同的ID。

我现在的问题是...难道posssible覆盖像ID RadioButtonFor的name属性??


解决方案

  

现在我的问题是...是posssible覆盖像ID RadioButtonFor的name属性??


没有,这是不可能的。 name属性会从你作为第一个参数传递拉姆达前pression计算。

我个人使用模板编辑器而不是在所有的回路

麻烦

  @model MainViewModel
<表>
    <&THEAD GT;
        ...
    < / THEAD>
    <&TBODY GT;
        @ Html.EditorFor(X => x.main)
    < / TBODY>
< /表>

和在相应的编辑模板:

  @model ChildViewModel
&所述; TR>
    &所述; TD宽度=30%>
        廉正
    < / TD>
    < TD WIDTH =17%>
        @ Html.RadioButtonFor(X => x.nested.integrity,1)差评
    < / TD>
    < TD WIDTH =18%>
        @ Html.RadioButtonFor(X => x.nested.integrity,2)良好
     < / TD>
     < TD WIDTH =18%>
         @ Html.RadioButtonFor(X => x.nested.integrity,3)优秀
     < / TD>
     < TD WIDTH =16%>
         @ Html.RadioButtonFor(X => x.nested.integrity,4)关
     < / TD>
< / TR>

I am using foreach loop insoide my view to display few radiobutton rows..

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>



Because i was getting problem while Model binding therefore i created manual id to suit my requirement(different incremented id ).
But again problems comes with name attribute i think. for first and every loop i am getting same name attribute(not incremented) i.e if i select radiobuttton from first loop then it deselect taht row from other loop.

Like

Loop1 id= "main_0__nested_integrity"
Loop2 id= "main_0__nested_integrity"
Loop1 name= "nested.integrity"
Loop2 name= "nested.integrity"

as you can see name attribute for all loops are same, with different id.
Now my Question is...Is it posssible to override name attribute of RadioButtonFor like id??

解决方案

Now my Question is...Is it posssible to override name attribute of RadioButtonFor like id??

No, that's not possible. The name attribute will be calculated from the lambda expression you passed as first argument.

Personally I would use editor templates and not bother with any loops at all

@model MainViewModel
<table>
    <thead>
        ...
    </thead>
    <tbody>
        @Html.EditorFor(x => x.main)
    </tbody>
</table>

and in the corresponding editor template:

@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>

这篇关于可能改变RadioButtonFor的名字吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆