在MVC 4剃刀多个单选按钮组 [英] Multiple radio button groups in MVC 4 Razor
问题描述
我需要有多个单选按钮组在我的形式是这样的:结果
我知道它只是通过指定相同完成的名称的为每个组HTML属性。结果
但结果
MVC不会让你使用HTML帮助像这样的时候指定自己的name属性:
@ Html.RadioButtonFor(I => item.id,item.SelectedID,新{名称= item.OptServiceCatId})
因为它看起来在每个标签的名称的属性(而不是的<em>标识的)来映射/绑定的形式向模型,控制器接收等。
有人说,指定具有相同的组名属性,就能解决问题,但它也不能工作。
那么,有哪些工作?
任何方式 编辑:结果
这是我的观点(简体):
@model Service_Provider.ViewModels.SelectOptServicesForSubServiceViewModel@foreach(在Model.OptServices VAR猫)
{
//一块code&放大器; html的在这里
@foreach(在cat.OptItems.Where(I =&GT VAR项目; i.MultiSelect == FALSE))
{
@ Html.RadioButtonFor(I =&GT; item.id,item.SelectedID,新{组名= item.OptServiceCatId})
&LT; BR /&GT;
}
}
注意:结果
我的模型是一个列表与LT; OptServices&GT;
:
公开名单&LT; OptServices&GT;猫{搞定;组;}
和OptServices有一个列表
的 OptItems
里面的:
公共类OptServices
{
//一些东西
公开名单&LT; OptItems&GT;项目{搞定;组;}
}
确定这里就是我如何解决了这个问题。
我的模型是的类别一列表
。每个类别包含一个列表
它的子。结果
考虑到这一点,在foreach循环中每一次,每个单选
将有它的类别的ID(这是唯一的),因为它是的名称的attribue。< BR>
而且我还用 Html.RadioButton
而不是 Html.RadioButtonFor
。
下面是最终的工作伪code:
@foreach(VAR猫Model.Categories)
{
//一块code&放大器; html的在这里
@foreach(在cat.SubCategories VAR项)
{
@ Html.RadioButton(item.CategoryID.ToString(),item.ID)
}
}
的结果是:
&LT;输入名称=127类型=无线电VALUE =110&GT;
请注意,我并没有把所有这些单选按钮组的表单中。我不知道,如果这个解决方案仍然在表单正常工作。
感谢所有谁帮我解决这个问题的人;)
I need to have multiple radio button groups in my form like this:
I know it's simply done by specifying the same "name" html attribute for each group.
HOWEVER
MVC doesn't let you specify your own name attribute when using html helper like this:
@Html.RadioButtonFor(i => item.id, item.SelectedID, new { Name = item.OptServiceCatId })
Because it looks at each tag's "name" attribute (not "id") to map/bind the form to the model which the controller receives, etc.
Some said that specifying each with the same "GroupName" attribute will solve the problem, but it didn't work either.
So, is there any way which works ?
EDIT:
Here's my view (simplified):
@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:
My model is a List<OptServices>
:
public List<OptServices> Cats {get; set;}
And OptServices has a List
of OptItems
inside:
public class OptServices
{
//a few things
public List<OptItems> Items {get; set;}
}
Ok here's how I fixed this
My model is a list
of categories. Each category contains a list
of it's subcategories.
with this in mind, every time in the foreach loop, each RadioButton
will have it's category's ID (which is unique) as it's name attribue.
And I also used Html.RadioButton
instead of Html.RadioButtonFor
.
Here's the final 'working' pseudo-code:
@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)
}
}
The result is:
<input name="127" type="radio" value="110">
Please note that I HAVE NOT put all these radio button groups inside a form. And I don't know if this solution will still work properly in a form.
Thanks to all of the people who helped me solve this ;)
这篇关于在MVC 4剃刀多个单选按钮组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!