1

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 ?

2
  • 2
    Why are you using byte for the TheProjects.Priority property, when the underlying type of PriorityForProject is int? It would be cleaner if they were the same. Commented Feb 25, 2015 at 17:18
  • yes I made them all same @JonSkeet thank you Commented Feb 25, 2015 at 17:31

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

yes You are right they are just core level of app. So as you said I did public PriorityForProject Priority { get; set; } thank you so much =)
3

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...)

7 Comments

well I am still learning. Why it is unwieldy could you please explain more and lead me to write clear and idiomatic code. Also it worked
@Jackal: It's the name - The is meaningless as part of a name, and Projects is odd when it's only a single project. It's not clear why you're got two representations of the Project type anyway, to be honest.
well I called it Projects because user will be added a lot of projects and I called it Projects because if I use Project for model and viewmodel sometimes it creates error because of the same name(I got that a few times).So when I need model I make it unique like PROJECT and for the wiew models I make it like Projects and OurProjects then I can find whatever I need without confusing myself after I look at the code
@Jackal: If you need to disambiguate, something like ProjectViewModel would make a lot more sense. Making it plural when in itself it's singular is just confusing (if you have a lot of projects, you'd have a List<Project> or whatever - and using Our, The etc is just unpredictable IMO.
Yea u are right. But I am only using it because they all should be unique name.I will keep what you say it in my mind. What should I use instead of Our and The ?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.