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

查看:37
本文介绍了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?

推荐答案

您可以为枚举 Airlines 创建自定义编辑器模板,该模板将呈现单选按钮列表.在您的模型中,您将拥有一个 Airlines 类型的属性,并使用 Required 属性标记该属性并设置 ErrorMessage = "select one item".如果需要,不要忘记为客户端验证包含 jQuery 验证,通常只需在您的布局或视图上添加 @Scripts.Render("~/bundles/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,位于 ViewsSharedEditorTemplates 目录中:

The Editor Template, Airlines.cshtml, in the ViewsSharedEditorTemplates 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天全站免登陆