2

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);
    }

1 Answer 1

3

As I was writing this question and checking my facts I discovered the very simple mistake I had made!

I had forgot to include the ID field on the edit page, so this was being set to zero in the posted model!

Lesson is...make sure all fields are represented on a form and if you don't want it displayed then have it as a hidden field!

Thank you for your help Stack Overflow ;-)

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for taking the time to post the answer after you found it. I changed tables and did not update my EditorTemplate properly and this got me on the right track.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.