我似乎发疯了,没有在MVC 3实体框架项目中获得更新的简单记录。我想念什么?
我可以很好地创建记录,但是似乎无法弄清楚执行更新的逻辑。我尝试了各种方法(附加,不附加),并认为当涉及到外键或其他东西时,我错过了一种模式。
视图返回的对象已完全填充,并且模型有效。填充外键ID字段。
有什么想法请。
POCO是
public class EFormApplication
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ApplicationId { get; set; }
[ForeignKey("ApplicationTemplateId")]
public virtual ApplicationTemplate ApplicationTemplate { get; set; }
public int ApplicationTemplateId { get; set; }
[Required(ErrorMessage = "Each application must have a Title")]
public string Title { get; set; }
[Required(ErrorMessage = "Each application must have a Description. This is visible to the clients")]
public string Description { get; set; }
[ForeignKey("SecurityId")]
public virtual Security Security { get; set; }
public int SecurityId { get; set; }
public bool Published { get; set; }
[Display(Name = "EIDV Required")]
public bool EIDVRequired { get; set; }
[Display(Name = "Investor confirmation required")]
public bool InvestorConfirmationRequired { get; set; }
[Display(Name = "Advisor confirmation required")]
public bool AdvisorConfirmationRequired { get; set; }
[Display(Name = "Payment required")]
public bool PaymentRequired { get; set; }
[Display(Name = "Login Required")]
public bool UserLoginRequired { get; set; }
[Display(Name = "ISA Trans allowed")]
public bool IsaTransfersAllowed { get; set; }
public DateTime? Deadline { get; set; }
[Display(Name = "Fully Subscribed")]
public bool FullySubscribed { get; set; }
[Display(Name = "Image Url")]
[RegularExpression(@"^(ht)tp(s?):\/\/[0-9a-zA-Z].+$", ErrorMessage = "You must enter a valid URI including the http:// or https://")]
public string ImageUrl { get; set; }
[Display(Name = "Information Url")]
[RegularExpression(@"^(ht)tp(s?):\/\/[0-9a-zA-Z].+$", ErrorMessage = "You must enter a valid URI including the http:// or https://")]
public string InformationUrl { get; set; }
public bool Discontinued { get; set; }
public DateTime Created { get; set; }
public string CreatedBy { get; set; }
public DateTime LastUpdated { get; set; }
public string LastUpdatedBy { get; set; }
//navigation
public virtual ICollection<ApplicationSubmission> ApplicationSubmissions { get; set; }
}
编辑控制器方法是
[Authorize(Roles = "Admins")]
public ActionResult Edit(int applicationId)
{
EFormApplication eformapplication = _formRepository.GetEFormApplication(applicationId);
return View(eformapplication);
}
[Authorize(Roles = "Admins")]
[HttpPost]
public ActionResult Edit(EFormApplication eformapplication)
{
if (ModelState.IsValid)
{
eformapplication.LastUpdated = DateTime.UtcNow;
eformapplication.LastUpdatedBy = User.Identity.Name;
var opstatus = _formRepository.UpdateEFormApplication(eformapplication);
if (opstatus.Status)
{
return RedirectToAction("Design", "eForm");
}
else
{
return RedirectToAction("OpStatusError", "Error", opstatus);
}
}
return View(eformapplication);
}
保存记录的位的存储库方法UpdateEFormApplication如下:
public OperationStatus UpdateEFormApplication(EFormApplication eFormApplication)
{
DataContext.Entry(eFormApplication).State = EntityState.Modified;
DataContext.SaveChanges();
return Save(eFormApplication);
}
当我写这个问题并检查事实时,我发现我犯了一个非常简单的错误!
我忘记了在编辑页面上包含ID字段,因此在发布的模型中将其设置为零!
教训是...确保所有字段都显示在表单上,如果您不希望显示它,则将其作为隐藏字段!
谢谢您的帮助Stack Overflow ;-)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句