【发布时间】:2023-03-08 03:44:01
【问题描述】:
下面是枚举叶子
public enum Leaves
{
Annual = 0,
Medical = 1,
Hospitalization = 2,
Unpaid = 3
}
下面是 linq 查询
public ActionResult ApproveLeave(int? id)
{
if (id == null)
return View();
LeaveApplication leaveApplication = db.LeaveApplication.Find(id);
if (leaveApplication == null)
return HttpNotFound();
leaveApplication.Status = "Approved";
if (ModelState.IsValid)
{
db.Entry(leaveApplication).State = EntityState.Modified;
Leaves leavesType = (Leaves)Enum.Parse(typeof(Leaves), leaveApplication.LeaveType.ToString());
var lb = (from t in db.LeaveBalance
select t)
.Where(t => t.LeaveType == leavesType && t.Profileid.Equals(id))
.FirstOrDefault();
lb.Taken++;
lb.Balance--;
db.SaveChanges();
return RedirectToAction("Index");
}
return View();
}
我什至尝试过使用 Leaves.Annual 但它不起作用。 LINQ 查询引发以下异常:
System.NotSupportedException HResult=0x80131515 消息=无法创建“System.Object”类型的常量值。此上下文仅支持原始类型或枚举类型。
【问题讨论】:
-
附带说明,您的
if (ModelState.IsValid)毫无意义 - 方法中的唯一参数是int?,因此ModelState不会无效 -
那么ModelState.Isvalid是基于我传入的方法的参数吗?
标签: c# asp.net-mvc entity-framework enums linq-to-entities