我有一个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方法的价值。从控制器中,如您所见,我可以获得返回数字的字节类型。我怎样才能达到它的价值?
答案 0 :(得分:3)
刚演员:
TheProjects x = ...;
PriorityForProject priority = (PriorityForProject) x.Priority;
(我强烈建议你重命名TheProjects
类型 - 这是一个非常笨拙,不引人注目的名字......)
答案 1 :(得分:3)
您必须转换为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; }
}
我认为枚举属于您的核心应用程序级别,因此引用不存在问题。