当我尝试使用MVC3在Edit视图中保存记录时,我会遇到奇怪的行为。 创建一个记录很好,没有什么不寻常的。如果我在“编辑”视图中打开相同的记录并对文本框进行更改,则单击“保存”并且记录不会更新。它可以通过断点看到ModelState未设置为有效。在那个断点中,我查看了模型中的键/值对,我看到两个看起来不应该存在的键。 这是我的模特:
public class Cases
{
//case data model for call center
//implement lists for all related child tables too
[Key]
public int CasesID { get; set; }
public string CaseNumber { get; set; }
[Required(ErrorMessage = "Customer is Required")]
public int CustomerID { get; set; }
public Customer Customer { get; set; }
[MaxLength(50)]
public string UserName { get; set; } //get user name from the aspnet membership
[Required(ErrorMessage = "Case Category is Required")]
public int CaseCategoryID { get; set; }
[Required(ErrorMessage = "Technician is Required")]
public int TechnicianID { get; set; }
public Technician Technicians { get; set; }
[Required(ErrorMessage = "Engine Model is Required")]
public int EngineModelID { get; set; }
public EngineModel EngineModel { get; set; }
[MaxLength(50)]
public string BMSWorkorder { get; set; }
[MaxLength(50)]
[Required(ErrorMessage = "Status is Required")]
public string CaseStatus { get; set; }
[MaxLength(50)]
public string OpenedBy { get; set; }
[Required(ErrorMessage = "Opened Date is Required")]
[DataType(DataType.DateTime)]
public DateTime? OpenedDate { get; set; }
[MaxLength(50)]
public string ClosedBy { get; set; }
[DataType(DataType.DateTime)]
public DateTime? ClosedDate { get; set; }
[MaxLength(50)]
[Required(ErrorMessage="Caller First Name is Required")]
public string CallerFirstName { get; set; }
[MaxLength(50)]
[Required(ErrorMessage = "Caller Last Name is Required")]
public string CallerLastName { get; set; }
[MaxLength(10)]
[Required(ErrorMessage = "Qualified is Required")]
public string Qualified { get; set; }
public string Description { get; set; }
[MaxLength(50)]
[Required(ErrorMessage = "ESN is Required")]
public string ESN { get; set; }
[MaxLength(50)]
[Required(ErrorMessage = "Mileage is Required")]
public string Mileage { get; set; }
[DataType(DataType.Date)]
public DateTime? DateInService { get; set; }
[MaxLength(50)]
public string ESTR { get; set; }
[MaxLength(50)]
[Required(ErrorMessage = "EDS is Required")]
public string EDS { get; set; }
[MaxLength(50)]
public string GensetSerialNumber { get; set; }
[MaxLength(50)]
public string GensetModelNumber { get; set; }
//child Case Notes records
public virtual ICollection<CaseNotes> CaseNotes { get; set; }
//child case attachment records
public virtual ICollection<Attachment> Attachments { get; set; }
//child case complaint records
public virtual ICollection<CaseComplaint> CaseComplaint { get; set; }
//tracking fields
public DateTime? CreatedOn { get; set; }
[MaxLength(50)]
public string CreatedBy { get; set; }
public DateTime? ModifiedOn { get; set; }
[MaxLength(50)]
public string ModifiedBy { get; set; }
}
问题似乎出现在Technicians导航属性中。 Technicians nav属性中的字段在我的模型状态中显示为键值对。如果它们中的任何一个都是空的,就像它们可能在任何时候一样,它们会显示为空键值对并导致模型状态无效。
这是技师模型:
public class Technician
{
public int TechnicianID { get; set; }
[MaxLength(50)]
[Required(ErrorMessage = "First Name is Required")]
public string FirstName { get; set; }
[MaxLength(50)]
[Required(ErrorMessage = "Last Name is Required")]
public string LastName { get; set; }
[MaxLength(50)]
public string PromoID { get; set; }
public int CustomerID { get; set; } //i dont think we need a nav property at this point 06-17-2012
public string AdditionalContact { get; set; }
}
我不明白这里发生了什么。为什么这些属性会阻止我的模型状态有效?
这是我的编辑控制器操作,用于打开编辑视图:
public ActionResult Edit(int id)
{
Cases cases = db.Cases.Find(id);
db.Entry(cases).Reference(x => x.Customer).Load();
db.Entry(cases).Collection(x => x.CaseComplaint).Load();
db.Entry(cases).Collection(x => x.CaseNotes).Load();
db.Entry(cases).Reference(x => x.Technicians).Load();
GetCaseCategoryLookup(cases.CaseCategoryID);
GetEngineModelLookup(cases.EngineModelID);
GetTechnicianLookup(cases.TechnicianID);
GetQualifiedList(cases.Qualified);
GetCaseStatusList(cases.CaseStatus);
return View(cases);
}
这是我的编辑控制器操作,让模型更新数据库。这是模型状态无效的地方。
[HttpPost]
public ActionResult Edit(Cases cases)
{
if (ModelState.IsValid)
{
AppHelpers help = new AppHelpers();
if (cases.CaseStatus == "CLOSED")
{
cases.ClosedBy = "USER";
cases.ClosedDate = help.GetEasternTime();
}
cases.ModifiedBy = "USER";
cases.ModifiedOn = help.GetEasternTime();
db.Cases.Add(cases);
db.Entry(cases).State = EntityState.Modified;
db.SaveChanges();
}
GetCaseCategoryLookup(cases.CaseCategoryID);
GetEngineModelLookup(cases.EngineModelID);
GetTechnicianLookup(cases.TechnicianID);
GetQualifiedList(cases.Qualified);
GetCaseStatusList(cases.CaseStatus);
return RedirectToAction("Index");
}
赖安