简体   繁体   中英

How to get an enum value ASP.NET MVC

I have a ProjectsModel as a 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; }
}

And I have Project as a model:

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; }
    }
}

As you see, I have the enum in my project.cs and here is my controller:

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);
}

I am trying to get PriorityForProject method's value. From the controller, as you see, I can get the byte type which returns a number. How can I reach the value of it ?

Just cast:

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

(I'd strongly advise you to rename the TheProjects type though - that's a really unwieldy, unidiomatic name...)

You must cast to the propertyforproject type (PriorityForProject) .

If you don't want to bother with values you can declare a PropertyForProject property in your model.

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; }

    }

I assume enums are in your core level of application, so there mustn't be a problem with references.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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