在MVC的foreach单选按钮 [英] mvc radiobuttons in foreach

查看:160
本文介绍了在MVC的foreach单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用MVC和我有一个foreach一些单选按钮:

I'm using MVC and I have some RadioButtons in a foreach:

    foreach (var item in group) {
                    <tr>
                        <td>
                            @Html.RadioButtonFor(modelItem => item.CheckerApproved, true)
                            @Html.RadioButtonFor(modelItem => item.CheckerApproved, false)
                        </td>
                    </tr>
}

因此​​,对于组中的每个项目应该有真假一个单选按钮。

So for every item in the group there should be a radiobutton for true and false.

问题是当有组中的多个项目,你可能最终为两项:

The problem is when there are multiple items in the group you might end up with for two items:

<tr>
   <td>
      <input id="item_CheckerApproved" type="radio" value="True" name="item.CheckerApproved">
      <input id="item_CheckerApproved" type="radio" value="False" name="item.CheckerApproved 
         checked="checked">
   </td>
</tr>

<tr>
   <td>
      <input id="item_CheckerApproved" type="radio" value="True" name="item.CheckerApproved">
      <input id="item_CheckerApproved" type="radio" value="False" name="item.CheckerApproved 
         checked="checked">
   </td>
</tr>

我想是的单选按钮是什么组是单独的。这样改变第一行中的值的第二行中不会影响的值。但是,如果您已设置说第一行中的真,那么选择第二行的第一行中的真实被取消任一值。

What I wanted was for the radiobuttons groups to be seperate. So changing the value in the first row won't effect the value in the second row. However if you have set say true in the top row then select either value in the second row the true in the first row is cancelled.

我相信这是因为它们是由name属性联系起来。但是你不能真正手动重置这个name属性。

I believe this is because they are linked by the name attribute. However you can't actually manually reset this name attribute.

有谁知道我怎么可能让它工作,使真假的每一行互相交流,但没有关于单选按钮在其他行有没有影响?

Does anyone know how I could make it work so the true and false for each row interact with each other but there is no effect on radiobuttons in other rows?

推荐答案

不要写在视图中循环。他们是丑陋和你结束了意外的行为,在你的问题中描述的。使用编辑器的模板,如:

Don't write loops in views. They are ugly and you end up with undesired behavior as the one described in your question. Use editor templates, like this:

<table>
    <thead>
        <tr>
            <th>Approved status</th>
        </tr>
    </thead>
    <tbody>
         @Html.EditorFor(x => x.Groups)
    </tbody>
</table>

在这里,我假定您的视图模型类型的群体集合属性的IEnumerable&LT; GroupViewModel&GT; 。现在,所有剩下的就是写这将为Groups集合中的每个元素(〜/查看/共享/ EditorTemplates / GroupViewModel.cshtml )来执行相应的编辑器模板:

Here I assume that your view model has a Groups collection property of type IEnumerable<GroupViewModel>. Now all that's left is to write the corresponding editor template which will be executed for each element in the Groups collection (~/Views/Shared/EditorTemplates/GroupViewModel.cshtml):

@model GroupViewModel
<tr>
    <td>
        @Html.RadioButtonFor(x => x.CheckerApproved, "true") <span>Approved</span>
        @Html.RadioButtonFor(x => x.CheckerApproved, "false") <span>Not approved</span>
    </td>
</tr>

现在,不仅你不再需要写在你看来丑陋的foreach循环,但你最终获得正确的命名约定。

Now not only that you no longer need to write any ugly foreach loops in your view but you end up with correct naming convention.

这篇关于在MVC的foreach单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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