我似乎疯狂地追逐我的尾巴没有在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);
}
答案 0 :(得分:2)
当我写这个问题并检查我的事实时,我发现了我犯过的一个非常简单的错误!
我忘记在编辑页面上包含ID字段,因此在发布的模型中将其设置为零!
课程是......确保所有字段都在表单上显示,如果您不希望它显示,则将其作为隐藏字段!
感谢您的帮助Stack Overflow; - )