相关疑难解决方法(0)

如何通过MVC剃刀代码获取Enum成员的显示名称属性?

我的模型中有一个名为"Promotion"的属性,它的类型是一个名为"UserPromotion"的标志枚举.我的枚举成员的显示属性设置如下:

[Flags]
public enum UserPromotion
{
    None = 0x0,

    [Display(Name = "Send Job Offers By Mail")]
    SendJobOffersByMail = 0x1,

    [Display(Name = "Send Job Offers By Sms")]
    SendJobOffersBySms = 0x2,

    [Display(Name = "Send Other Stuff By Sms")]
    SendPromotionalBySms = 0x4,

    [Display(Name = "Send Other Stuff By Mail")]
    SendPromotionalByMail = 0x8
}
Run Code Online (Sandbox Code Playgroud)

现在我希望能够在我的视图中创建一个ul来显示我的"Promotion"属性的选定值.这是我到目前为止所做的,但问题是如何在这里获取显示名称?

<ul>
    @foreach (int aPromotion in @Enum.GetValues(typeof(UserPromotion)))
    {
        var currentPromotion = (int)Model.JobSeeker.Promotion;
        if ((currentPromotion & aPromotion) == aPromotion)
        {
        <li>Here I don't know how to get the display attribute of …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc displayattribute razor

185
推荐指数
12
解决办法
23万
查看次数

MVC5:Enum单选按钮,标签为displayname

我有这些枚举

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
}
Run Code Online (Sandbox Code Playgroud)

我有一个帮助类来显示枚举中每个字段的所有单选按钮

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

现在标签设置为值名称,而不是我为值给出的显示名称.

例如:

[Display(Name="Cancel Invitation")]
CancelInvitation
Run Code Online (Sandbox Code Playgroud)

CancelInvitation旁边有单选按钮.

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

c# asp.net-mvc enums asp.net-mvc-5

14
推荐指数
1
解决办法
1万
查看次数

在剃刀视图中显示枚举描述

我有一个枚举属性的模型如下:

namespace ProjectManager.Models
{
    public class Contract
    {
        .....
        public enum ContractStatus
        {
            [System.ComponentModel.Description("????")]
            New,
            [System.ComponentModel.Description("?? ?????? ??????")]
            WaitForPayment,
            [System.ComponentModel.Description("?????? ???")]
            Paid,
            [System.ComponentModel.Description("????? ?????")]
            Finished
        };

        public ContractStatus Status { get; set; }
        .....
    }

}
Run Code Online (Sandbox Code Playgroud)

在我的剃刀视图中,我想显示每个项目的枚举描述,例如,????而不是New.我试着按照这个答案中的说明,但我不知道在哪里添加扩展方法以及如何在我的razor视图文件中调用扩展方法.如果有人能完成我的代码,我将感激不尽:

@model IEnumerable<ProjectManager.Models.Contract>
....
<table class="table">
    <tr>
        .....
        <th>@Html.DisplayNameFor(model => model.Status)</th>
        .....
    </tr>

@foreach (var item in Model) {
    <tr>
        ......
        <td>
            @Html.DisplayFor(modelItem => item.Status)  //<---what should i write here?
        </td>
        ....
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.Id …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc enums data-annotations razor

2
推荐指数
1
解决办法
3115
查看次数