MVC4枚举和单选按钮列表 [英] MVC4 enum and radio button list

查看:124
本文介绍了MVC4枚举和单选按钮列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到的这几个线程,但没有似乎适用于MVC4因为RadioButtonFor HTML扩展方法/帮手不存在。

I have seen a few threads on this but none seem to apply to MVC4 because the RadioButtonFor html extension method/helper does not exist.

说我有一个枚举列表 - 即航空:

Say I have an enum list - i.e Airlines:

public enum Airlines
{
   Unknown = 0,
   BritishAirways = 1,
   VirginAtlantic = 2,
   AirFrance = 3
}

我如何绑定这一个单选按钮列表上的我的看法,并能够获取选中的项目吗?
怎么样能够说选择一个项目,如果没有选择国产?

How can I bind this to a Radio button list on my view and be able to retrieve the selected item? What about being able to say "select one item" if there is no selection made?

推荐答案

您可以创建枚举航空公司自定义编辑模板将呈现一个单选按钮列表。在模型中,你将有键入航空和标记这个属性与要求属性,并设置<$ C的属性$ C>的ErrorMessage =选择一个项目。不要忘了包括客户端验证的jQuery验证,如果你想要的话,通常只需要添加 @ Scripts.Render(〜/包/ jqueryval)上您的布局或视图。如果你不使用jQuery验证,你将需要在模型上的财产空,因为枚举只是默认设置为第一个值,以便MVC不会看到它视为无效。请记住,如果你改变可空,你还需要改变模型类型的编辑模板可空以及属性。

You can create a custom Editor Template for the enum Airlines that will render a radio button list. In your Model you will have a property of type Airlines and tag this property with the Required attribute and set ErrorMessage = "select one item". Don't forget to include the jQuery validation for client side validation if you want it, usually just have to add @Scripts.Render("~/bundles/jqueryval") on your Layout or View. If you don't use the jQuery validation you will need to make the property nullable on the model because enums are just set to the first value by default so MVC will not see it as invalid. Remember if you change the property to nullable you will also need to change the Model type of your Editor Template to nullable as well.

更新

要启用编辑模板来渲染一个单选按钮列表中的任何枚举,模板更改为以下内容:

To enable the Editor Template to render a radio button list for any enum, change the template to the following:

@model Enum
@foreach (var value in Enum.GetValues(Model.GetType()))
{
    @Html.RadioButtonFor(m => m, value)
    @Html.Label(value.ToString())
}

原始

编辑器模板的 Airlines.cshtml 的,在视图\\共享\\ EditorTemplates 的目录:

The Editor Template, Airlines.cshtml, in the Views\Shared\EditorTemplates directory:

@model MvcTest.Models.Airlines
@foreach (var value in Enum.GetValues(typeof(MvcTest.Models.Airlines)))
{
    @Html.RadioButtonFor(m => m, value)
    @Html.Label(value.ToString())
}

示范:

public class TestModel
{
    [Required(ErrorMessage = "select one item")]
    public Airlines Airline { get; set; }
}

动作方法:

public class HomeController : Controller
{
    [HttpGet]
    public ActionResult Index()
    {
        return View(new TestModel());
    }

    [HttpPost]
    public ActionResult Index(TestModel model)
    {
        if (ModelState.IsValid)
        {
            return RedirectToAction("Index");
        }

        return View(model);
    }
}

视图:

@model MvcTest.Models.TestModel
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Index</h2>
@using (Html.BeginForm())
{
    @Html.EditorFor(m => m.Airline)
    <input type="submit" value="Submit" />
    @Html.ValidationSummary(false)
}

这篇关于MVC4枚举和单选按钮列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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