我有一个 ProjectsModel 作为 View 模型:
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 中有枚举,这是我的 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);
}
我正在尝试获取 PriorityForProject 方法的值。从 Controller 中,如您所见,我可以获得返回数字的字节类型。我怎样才能达到它的值(value)?
最佳答案
您必须转换为项目类型 (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; }
}
我假设枚举在您的核心应用程序中,因此引用一定没有问题。
关于c# - 如何获取枚举值 ASP.NET MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28725370/