如何在ASP.NET MVC中使用带有DescriptionAttribute的枚举 [英] how to use enum with DescriptionAttribute in asp.net mvc

查看:116
本文介绍了如何在ASP.NET MVC中使用带有DescriptionAttribute的枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是asp.net MVC的新手。我正在尝试使用从枚举中填充的视图页面上的下拉控件。我还想向下拉值添加自定义描述。我搜索了很多示例,但没有人在视图页面上发布如何填充描述的信息。这是我的代码:

I am new to asp.net MVC. I am trying to use dropdown control on my view page, which populates from enum. I also want to add custom descriptions to dropdown values. I searched so many examples, but no one posted how to populated description on view page. Here is my code:

ViewModel:

ViewModel:

public enum SearchBy
    {
        [Description("SID/PID")]
        SID = 1,
        [Description("Name")]
        Name,
        [Description("Birth Date")]
        DOB,
        [Description("Cause#")]
        Cause
    }

Index.cshtml

Index.cshtml

<div class="form-horizontal">
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group form-inline">
        @Html.LabelFor(model => model.searchBy, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EnumDropDownListFor(model => model.searchBy, "Search By", htmlAttributes: new { @class = "form-control" })
            @Html.TextBox("searchByVal", null, htmlAttributes: new { @placeholder = "SID / PID ", @class = "form-control" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @placeholder = "First Name", @class = "form-control" } })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.LastName, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @placeholder = "Last Name", @class = "form-control" } })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.DOB, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.DOB, new { htmlAttributes = new { @placeholder = "Birth Date", @class = "form-control" } })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.CauseNumber, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.CauseNumber, new { htmlAttributes = new { @placeholder = "Cause#", @class = "form-control" } })
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Search" class="btn btn-block btn-primary" />
        </div>
    </div>
</div>

它没有像我的SearchBy枚举中所描述的那样填充描述字段。看到图像在这里。
http://postimg.org/image/phdxgocj7/
请帮助我,我在哪里犯错。谢谢

It is not populating the with description fields as mentioned in my SearchBy enum. see the image here. http://postimg.org/image/phdxgocj7/ Please help me, where I am making mistake. Thank you

更新
我从Nico获得了解决方案。我对此做了一点研究。我正在用解决方案更新此帖子,因为它可能对MVC新手
http://weblogs.asp.net/jongalloway//looking-at-asp- net-mvc-5-1-and-web-api-2-1-part-1-概述和枚举

谢谢大家。享受编码的乐趣。

Thank you all. Enjoy coding..

推荐答案

HTML帮助器 EnumDropDownListFor EnumDropDownList 没有考虑 enum 成员上的 Description 属性修饰。但是,通过查看源代码:

The Html helper EnumDropDownListFor or EnumDropDownList does not take into consideration the Description attribute decorations on the enum members. However by reviewing the source code:

枚举下拉列表帮助器:
https://aspnetwebstack.codeplex.com/SourceControl/latest#src/System.Web.Mvc/Html/SelectExtensions.cs

Enum Dropdown List Helper: https://aspnetwebstack.codeplex.com/SourceControl/latest#src/System.Web.Mvc/Html/SelectExtensions.cs

枚举帮助器类:
https://aspnetwebstack.codeplex.com/SourceControl/latest#src/System.Web.Mvc/Html/EnumHelper.cs

The上面的枚举帮助器类用于将 Enum 转换为 List< SelectListItem> 。从下面的代码中:

The enum helper classes above are used to convert an Enum to a List<SelectListItem>. From the code below:

// Return non-empty name specified in a [Display] attribute for the given field, if any; field's name otherwise
private static string GetDisplayName(FieldInfo field)
{
    DisplayAttribute display = field.GetCustomAttribute<DisplayAttribute>(inherit: false);
    if (display != null)
    {
        string name = display.GetName();
        if (!String.IsNullOrEmpty(name))
        {
            return name;
        }
    }

    return field.Name;
}

您可以在 GetDisplayName方法中看到会检查 enum 成员上是否存在 DisplayAttribute 。如果显示属性存在,则将名称设置为 DisplayAttribute.GetName()方法的结果。

You can see that in the method GetDisplayName it checks for the existence of the DisplayAttribute on the enum member. If the display attribute exists then the name is set to the result of DisplayAttribute.GetName() method.

将其放在一起,我们可以修改枚举以使用 DisplayAttribute 代替 DescriptionAttribute 并将 Name 属性设置为要显示的值。

Putting this together we can modify the enum to use the DisplayAttribute instead of the DescriptionAttribute and setting the Name property to the value you wish to display.

public enum SearchBy
{
    [Display(Name = "SID/PID")]
    SID = 1,
    [Display(Name = "Name")]
    Name,
    [Display(Name = "Birth Date")]
    DOB,
    [Display(Name = "Cause#")]
    Cause
}

希望这会有所帮助。

这篇关于如何在ASP.NET MVC中使用带有DescriptionAttribute的枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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