使用HtmlHelper类时,MVC单选按钮列表不进行分组 [英] MVC Radio Button Lists are not grouped when using the HtmlHelper class

查看:157
本文介绍了使用HtmlHelper类时,MVC单选按钮列表不进行分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

遇到问题创造组合在一起,在MVC 3特别单选按钮的列表,但是这也适用于MVC 2。

Having trouble creating a list of radio buttons that are grouped together, in MVC 3 specifically, but this also applies to MVC 2.

在使用HTML辅助生成单选按钮,就会出现问题和模型是一个数组的一部分。

The problem arises when radio buttons are generated using Html helpers and the model is part of an array.

下面是我的code的削减版本。

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" } 
}

我的父视图

@model CollectionOfStuff

@for (int i = 0; i < Model.Things.Length; i++) {
    @Html.EditorFor(m => m.Things[i]);
}

我的视图渲染个别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)     

产地:

<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.

另外其他选项创建一个自定义模板,但这种方法的问题是,所有的验证所需的元数据不是present。

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?

推荐答案

您还没有表现出请问你的视图模型的样子,但你可以通过一些属性组他们。所以,让我们看一个例子:

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; }
}

控制器:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View(new MyViewModel());
    }

    [HttpPost]
    public ActionResult Index(MyViewModel model)
    {
        return View(model);
    }
}

查看:

@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" />
}

现在,如果你想preSELECT一些电台干脆设置视图模型无线电相应价值的财产,而不是在你的看法写一些丑陋的C#code的:

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:

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.

这篇关于使用HtmlHelper类时,MVC单选按钮列表不进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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