asp.net-mvc 使用 HtmlHelper 类时,MVC 单选按钮列表未分组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5289489/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MVC Radio Button Lists are not grouped when using the HtmlHelper class
提问by Vince Panuccio
Having trouble creating a list of radio buttons that are grouped together, in MVC 3 specifically, but this also applies to MVC 2.
在创建组合在一起的单选按钮列表时遇到问题,特别是在 MVC 3 中,但这也适用于 MVC 2。
The problem arises when radio buttons are generated using Html helpers and the model is part of an array.
当使用 Html 帮助程序生成单选按钮并且模型是数组的一部分时,就会出现问题。
Here is the cut down version of my code.
这是我的代码的简化版本。
public class CollectionOfStuff {
public MVCModel[] Things { get; set }
}
/*This model is larger and represents a Person*/
public class MVCModel {
[UIHint("Hidden")]
public string Id { get; set; }
public string Name { get; set; }
public bool IsSelected { get; set; }
}
/*Assigned to new CollectionOfStuff property Things*/
var items = new[] {
new MVCModel() { Id="0" Name = "Name here" }, new MVCModel() { Id="1" Name = "Name there" }
}
My parent view
我的父视图
@model CollectionOfStuff
@for (int i = 0; i < Model.Things.Length; i++) {
@Html.EditorFor(m => m.Things[i]);
}
My view rendering individual MVCModel objects
我的视图呈现单个 MVCModel 对象
@Model MVCModel
@{
var attr = new {
Checked = Model.IsSelected ? "checked=checked" : ""
};
}
@Html.RadioButtonFor(model => model, Model.Id, attr)
Produces this output:
产生这个输出:
<input type="radio" value="0" name="MVCModel[0]" id="MVCModel_0_" data-val-required="You need to choose" data-val="true" />
<input type="radio" value="1" name="MVCModel[1]" id="MVCModel_1_" data-val-required="You need to choose" data-val="true" />
The radio buttons are not grouped, however it has the obvious advantage of writing out the meta data for validation.
单选按钮没有分组,但是它具有写出元数据进行验证的明显优势。
The other way is by calling:
另一种方式是调用:
@Html.RadioButton(name: "GroupName", value: Model.Id, isChecked: Model.IsSelected)
Produces:
产生:
<input type="radio" value="0" name="MVCModel[0].GroupName" id="MVCModel_0__GroupName">
<input type="radio" value="1" name="MVCModel[1].GroupName" id="MVCModel_1__GroupName">
Again, this doesn't produce the desired result. It's also missing the validation meta data.
同样,这不会产生预期的结果。它还缺少验证元数据。
Another other option is creating a custom template, but the problem with this approach is that all the meta data required for validation is not present.
另一种选择是创建自定义模板,但这种方法的问题是验证所需的所有元数据都不存在。
Any ideas on how I can create grouped radio buttons or obtain meta data so I can create a template myself?
关于如何创建分组单选按钮或获取元数据以便我可以自己创建模板的任何想法?
回答by Darin Dimitrov
You haven't shown how does your view model look like but you could group them by some property. So let's take an example:
您还没有展示您的视图模型的外观,但您可以按某些属性对它们进行分组。让我们举个例子:
public class MyViewModel
{
[Required]
public string SomeProperty { get; set; }
}
Controller:
控制器:
public class HomeController : Controller
{
public ActionResult Index()
{
return View(new MyViewModel());
}
[HttpPost]
public ActionResult Index(MyViewModel model)
{
return View(model);
}
}
View:
看法:
@model AppName.Models.MyViewModel
@using (Html.BeginForm())
{
<div>A: @Html.RadioButtonFor(x => x.SomeProperty, "a")</div>
<div>B: @Html.RadioButtonFor(x => x.SomeProperty, "b")</div>
@Html.ValidationMessageFor(x => x.SomeProperty)
<input type="submit" value="OK" />
}
Now if you want to preselect some radio simply set the property of the view model to the corresponding value of the radio instead of writing some ugly C# code in your views:
现在,如果您想预先选择某个收音机,只需将视图模型的属性设置为收音机的相应值,而不是在您的视图中编写一些难看的 C# 代码:
public ActionResult Index()
{
var model = new MyViewModel
{
SomeProperty = "a" // select the first radio
};
return View(model);
}
Obviously this technique works with any simple property type (not only strings) and with any number of radio buttons that could be associated to this property.
显然,这种技术适用于任何简单的属性类型(不仅是字符串)以及可以与该属性关联的任意数量的单选按钮。