Ali*_*Ali 37 radio-group radio-button razor asp.net-mvc-4
我需要在我的表单中有多个单选按钮组,如下所示:
我知道只需为每个组指定相同的" name "html属性即可.
但是,
当使用html helper时,MVC不允许您指定自己的name属性:
@Html.RadioButtonFor(i => item.id, item.SelectedID, new { Name = item.OptServiceCatId })
Run Code Online (Sandbox Code Playgroud)
因为它查看每个标签的" 名称 "属性(而非" id ")以将表单映射/绑定到控制器接收的模型等.
有人说,指定每个具有相同"GroupName"属性将解决问题,但它也不起作用.
那么,有什么办法可行吗?
编辑:
这是我的观点(简化):
@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 />
}
}
Run Code Online (Sandbox Code Playgroud)
注意:
我的模型是List<OptServices>:
public List<OptServices> Cats {get; set;}
Run Code Online (Sandbox Code Playgroud)
和OptServices拥有List的OptItems内部:
public class OptServices
{
//a few things
public List<OptItems> Items {get; set;}
}
Run Code Online (Sandbox Code Playgroud)
Mat*_*ily 29
您所需要的只是将组绑定到模型中的其他项目
@Html.RadioButtonFor(x => x.Field1, "Milk")
@Html.RadioButtonFor(x => x.Field1, "Butter")
@Html.RadioButtonFor(x => x.Field2, "Water")
@Html.RadioButtonFor(x => x.Field2, "Beer")
Run Code Online (Sandbox Code Playgroud)
Ali*_*Ali 19
好的,这是我如何解决这个问题
我的模型是一个list的类别.每个类别包含list其子类别.
考虑到这一点,每次在foreach循环中,每个都RadioButton将其类别的ID(这是唯一的)作为其名称属性.
我也用Html.RadioButton而不是Html.RadioButtonFor.
这是最终的'工作'伪代码:
@foreach (var cat in Model.Categories)
{
//A piece of code & html here
@foreach (var item in cat.SubCategories)
{
@Html.RadioButton(item.CategoryID.ToString(), item.ID)
}
}
Run Code Online (Sandbox Code Playgroud)
结果是:
<input name="127" type="radio" value="110">
Run Code Online (Sandbox Code Playgroud)
请注意,我没有将所有这些单选按钮组放在表单中.而且我不知道这个解决方案是否仍能在表单中正常运行.
感谢所有帮助我解决这个问题的人;)
| 归档时间: |
|
| 查看次数: |
133396 次 |
| 最近记录: |