繁体   English   中英

如何获得枚举值ASP.NET MVC

[英]How to get an enum value ASP.NET MVC

我有一个ProjectsModel作为viewmodel:

public IEnumerable<TheProjects> OurProjects { get; set; }

public class TheProjects
{
    public int Id { get; set; }

    public string ProjectName { get; set; }
    public short PhaseCount { get; set; }
    public string ProjectOwner { get; set; }
    public byte Priority { get; set; }
}

我将Project作为模型:

public enum PriorityForProject
{
    Acil = 0,
    Normal = 1,
    Düsük = 2
}

namespace JobTracking.Models
{
    public class Project
    {
        public int Id { get; set; }
        public string ProjectName { get; set; }
        public int ProjectNo { get; set; }
        public string ProjectType { get; set; }
        public string ProjectOwner { get; set; }
        public string StartDate { get; set; }
        public string EndDate { get; set; }
        public string ProjectManager { get; set; }
        public string Team { get; set; }
        public string ProjectDescription { get; set; }
        public PriorityForProject Priority { get; set; }
        public string Costs { get; set; }
        public string CashExpenditures { get; set; }
        public short Sira { get; set; }
    }
}

如您所见,我的project.cs中有枚举,这是我的控制器:

public ActionResult Index(int S = 1)
{
    var itemsPerPage = 100;
    var model = new ProjectsModel
    {
        ItemsPerPage = itemsPerPage,
        Page = S,
        TotalItems = Db.MyProjects.Count(),
        OurProjects = Db.MyProjects.OrderByDescending(o => o.Sira).Skip((S - 1) * itemsPerPage).Take(itemsPerPage)
        .Select(s => new ProjectsModel.TheProjects
        {
            Id = s.Id,
            ProjectName = s.ProjectName,
            PhaseCount = (short)Db.MyPhases.Where(p => p.Project.Id == s.Id).Count(),
            ProjectOwner=s.ProjectOwner,
            Priority = (byte)s.Priority
        })
    };
    return View(model);
}

我正在尝试获取PriorityForProject方法的值。 如您所见,从控制器中,我可以获取返回数字的字节类型。 我如何达到它的价值?

刚投:

TheProjects x = ...;
PriorityForProject priority = (PriorityForProject) x.Priority;

(不过,我强烈建议您重命名TheProjects类型-这是一个非常笨拙的名称,...)

您必须强制转换为propertyforproject类型(PriorityForProject)

如果您不想使用值,可以在模型中声明PropertyForProject属性。

public class TheProjects
    {
        public int Id { get; set; }
        public string ProjectName { get; set; }
        public short PhaseCount { get; set; }
        public string ProjectOwner { get; set; }
        public byte PriorityValue { get; set; } /* check if you really need this */
        public PriorityForProject Priority { get; set; }

    }

我假设枚举位于您的应用程序的核心级别,因此引用一定不会有问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM