I need to have multiple radio button groups in my form like this:
我知道这只是通过指定相同的“name" 每个组的 html 属性。
HOWEVER
在使用 html 帮助器时,MVC 不允许您指定自己的 name 属性,如下所示:
@Html.RadioButtonFor(i => item.id, item.SelectedID, new { Name = item.OptServiceCatId })
Because它查看每个标签的“name“属性(不是”id") 将表单映射/绑定到控制器接收的模型等。
有人说指定每个具有相同的“GroupName”属性可以解决问题,但它也不起作用。
那么,有什么办法行之有效吗?
EDIT:
这是我的观点(简化):
@model Service_Provider.ViewModels.SelectOptServicesForSubServiceViewModel
@foreach (var cat in Model.OptServices)
{
//A piece of code & html here
@foreach (var item in cat.OptItems.Where(i => i.MultiSelect == false))
{
@Html.RadioButtonFor(i => item.id, item.SelectedID, new { GroupName = item.OptServiceCatId })
<br />
}
}
NOTE:
我的模型是一个List<OptServices>
:
public List<OptServices> Cats {get; set;}
OptServices 有一个List
of OptItems
inside:
public class OptServices
{
//a few things
public List<OptItems> Items {get; set;}
}