MVC5: 枚举类型单选按钮,标签显示为枚举名称

16

我有这些枚举。

public enum QuestionStart
{
    [Display(Name="Repeat till common match is found")]
    RepeatTillCommonIsFound,

    [Display(Name="Repeat once")]
    RepeatOnce,    

    [Display(Name="No repeat")]
    NoRepeat

}

public enum QuestionEnd
{
    [Display(Name="Cancel Invitation")]
    CancelInvitation,

    [Display(Name="Plan with participants on first available common date")]
    FirstAvailableCommon,

    [Display(Name="Plan with participants on my first available common date")]
    YourFirstAvailableCommon
}

而且我有一个帮助类,用于显示枚举中每个字段的所有单选按钮。

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

现在标签的值是名称而不是我为值给定的显示名称。

例如:

[Display(Name="Cancel Invitation")]
CancelInvitation

我有一个带有CancelInvitation标签的单选按钮。

如何使它显示我给定的显示名称(Cancel Invitation)?


这段代码是为MVC5设计的吗? - Alex
我应该在哪里实现那个函数?我的视图使用Html.EditorFor,而我有一个帮助类来显示单选按钮。 - Cybercop
检查 packages.config 文件以获取 MVC 版本。 - ramiramilu
我需要单选按钮,而不是下拉列表 :( - Cybercop
哦,没问题,我明白了。抱歉让你走了弯路。我会删除我的评论。 - ramiramilu
显示剩余4条评论
4个回答

31

以下是解决方案 -

感谢这位杰出的绅士ThumNet,他编写了一个将枚举转换为RadioButtonList的扩展程序

步骤1 -Views/Shared/EditorTemplates 目录下创建 RadioButtonListEnum.cshtml 文件,并添加以下代码(代码来自上述参考链接)-

@model Enum

@{
     // Looks for a [Display(Name="Some Name")] or a [Display(Name="Some Name", ResourceType=typeof(ResourceFile)] Attribute on your enum
    Func<Enum, string> getDescription = en =>
    {
        Type type = en.GetType();
        System.Reflection.MemberInfo[] memInfo = type.GetMember(en.ToString());

        if (memInfo != null && memInfo.Length > 0)
        {

            object[] attrs = memInfo[0].GetCustomAttributes(typeof(System.ComponentModel.DataAnnotations.DisplayAttribute),
                                                            false);

            if (attrs != null && attrs.Length > 0)
                return ((System.ComponentModel.DataAnnotations.DisplayAttribute)attrs[0]).GetName();
        }

        return en.ToString();
    };
    var listItems = Enum.GetValues(Model.GetType()).OfType<Enum>().Select(e =>
    new SelectListItem()
    {
        Text = getDescription(e),
        Value = e.ToString(),
        Selected = e.Equals(Model)
    });
    string prefix = ViewData.TemplateInfo.HtmlFieldPrefix;
    int index = 0;
    ViewData.TemplateInfo.HtmlFieldPrefix = string.Empty;

    foreach (var li in listItems)
    {
        string fieldName = string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0}_{1}", prefix, index++);
        <div class="editor-radio">
        @Html.RadioButton(prefix, li.Value, li.Selected, new { @id = fieldName }) 
        @Html.Label(fieldName, li.Text)    
        </div>
    }
    ViewData.TemplateInfo.HtmlFieldPrefix = prefix;
}

那么请定义您的枚举 -

public enum QuestionEnd
{
    [Display(Name = "Cancel Invitation")]
    CancelInvitation,

    [Display(Name = "Plan with participants on first available common date")]
    FirstAvailableCommon,

    [Display(Name = "Plan with participants on my first available common date")]
    YourFirstAvailableCommon
}

第二步 - 创建模型 -

public class RadioEnumModel
{
    public QuestionEnd qEnd { get; set; }
}

步骤 3 - 创建控制器动作 -

    public ActionResult Index()
    {
        RadioEnumModel m = new RadioEnumModel();
        return View(m);
    }

第四步 - 创建视图 -

@model MVC.Controllers.RadioEnumModel
@Html.EditorFor(x => x.qEnd, "RadioButtonListEnum")
那么输出结果将是 -

enter image description here


似乎可以工作。谢谢!如果未来版本的MVC中有内置的辅助程序来处理这些情况,那就太好了。但是无论如何,我会投赞成票并接受答案! - Cybercop
1+ for your effort :) - Syed Ali

2
我发现一些答案令人困惑,这是我最终达到结果的方法。希望这能帮助其他人。
将此代码放入扩展方法文件中:
public static string GetDescription(this Enum value)
{
    Type type = value.GetType();
    string name = Enum.GetName(type, value);
    if (name != null)
    {
        FieldInfo field = type.GetField(name);
        if (field != null)
        {
            DescriptionAttribute attr =
                   Attribute.GetCustomAttribute(field,
                     typeof(DescriptionAttribute)) as DescriptionAttribute;
            if (attr != null)
            {
                return attr.Description;
            }
        }
    }
    return null;
}

请确保在您实际的模型中添加与枚举相同类型的属性,以便将单选按钮选择绑定。

为您特定的枚举创建编辑器模板。然后在您的视图中引用它,例如:

@Html.EditorFor(m => m.MyEnumTypePropertyName)

然后将以下内容添加到您的EditorTemplate中:
@using MyProject.ExtensionMethods;
@model MyProject.Models.Enums.MyEnumType

    @foreach (MyEnumType value in Enum.GetValues(typeof(MyEnumType)))
    {
        <div>
            @Html.Label(value.GetDescription())
            @Html.RadioButtonFor(m => m, value)
        </div>
    }

请注意 - 如果由于任何原因您没有选择单选按钮,当提交时,您的枚举属性的默认值将始终是包中的第一个(即零),而不是null。


0

这里有一个解决方案,它使用扩展方法和编辑器模板来创建一个带有本地化名称的单选按钮组,该名称来自于显示属性。

扩展方法

public static string DisplayName(this Enum enumValue)
{
    var enumType = enumValue.GetType();
    var memberInfo = enumType.GetMember(enumValue.ToString()).First();

    if (memberInfo == null || !memberInfo.CustomAttributes.Any()) return enumValue.ToString();

    var displayAttribute = memberInfo.GetCustomAttribute<DisplayAttribute>();

    if (displayAttribute == null) return enumValue.ToString();

    if (displayAttribute.ResourceType != null && displayAttribute.Name != null)
    {
        var manager = new ResourceManager(displayAttribute.ResourceType);
        return manager.GetString(displayAttribute.Name);
    }

    return displayAttribute.Name ?? enumValue.ToString();
}

例子

public enum IndexGroupBy 
{
    [Display(Name = "By Alpha")]
    ByAlpha,
    [Display(Name = "By Type")]
    ByType
}

以下是它的使用方法:

@IndexGroupBy.ByAlpha.DisplayName()

编辑器模板

这是一个可以与上述扩展方法一起使用的编辑器模板:

@model Enum

@{    
    var listItems = Enum.GetValues(Model.GetType()).OfType<Enum>().Select(e =>
        new SelectListItem
        {
            Text = e.DisplayName(),
            Value = e.ToString(),
            Selected = e.Equals(Model)
        });
    var prefix = ViewData.TemplateInfo.HtmlFieldPrefix;
    var index = 0;
    ViewData.TemplateInfo.HtmlFieldPrefix = string.Empty;

    foreach (var li in listItems)
    {
        var fieldName = string.Format(CultureInfo.InvariantCulture, "{0}_{1}", prefix, index++);
        <div class="editor-radio">
            @Html.RadioButton(prefix, li.Value, li.Selected, new {@id = fieldName})
            @Html.Label(fieldName, li.Text)
        </div>
    }
    ViewData.TemplateInfo.HtmlFieldPrefix = prefix;
}

以下是一个使用示例:

@Html.EditorFor(m => m.YourEnumMember, "Enum_RadioButtonList")

0
您可以使用以下的重载方法。
@Html.Label(value.ToString(),"Cancel Invitation")

这将使用上述调用中提供的第二个参数指定的标签文本来呈现标签。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接