I seem to be going nuts chasing my tail not getting a simple record to update in an MVC 3 entity framework project. What am I missing?
I can create records fine, but can't seem to work out the logic to perform an update. I have tried a variety of methods, attaching, not attaching, and am thinking that I have missed a pattern when foreign keys are involved or something.
The object returned by the view is fully populated and the model is valid. The foreign key id fields are populated.
Any ideas please.
The POCO is
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; }
}
The Edit controller methods are
[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);
}
And the repository method UpdateEFormApplication which is the bit that save the record is as follows:
public OperationStatus UpdateEFormApplication(EFormApplication eFormApplication)
{
DataContext.Entry(eFormApplication).State = EntityState.Modified;
DataContext.SaveChanges();
return Save(eFormApplication);
}