Question

Im trying to update an entry with EF5 with the following actionresult:

[HttpPost]
    public ActionResult Edit(int id, IEnumerable<HttpPostedFileBase> files)
    {
        if (ModelState.IsValid)
        {
            Reference reference = db.References.Single(x => x.Id == id);
            db.Entry(reference).State = EntityState.Modified;
            db.SaveChanges();
            //Other stuff regarding files/images

            return RedirectToAction("Index");
        }
        return View();
     }

Nothing happens. When I debug it, it goes trough the code as everything was fine. But nothing is updated in the db.

Here's the model if needed:

public class Reference
    {
        public int Id { get; set; }
        public string Headline { get; set; }
        public string Text { get; set; }
        public DateTime Date { get; set; }
        public IEnumerable<HttpPostedFileBase> ImageUploadMain { get; set; } 
        public String MainFileName { get; set; }
        public IEnumerable<HttpPostedFileBase> ImageUpload { get; set; }
        public virtual ICollection<Image> Files { get; set; }
        public virtual ICollection<RefProperties> Properties { get; set; }
        public int CategoryId { get; set; }
        public virtual Category Category { get; set; }
}

public class Image
{
    public int Id { get; set; }
    public string FileName { get; set; }
    public virtual Reference Reference { get; set; }
}

public class RefProperties
{
    public int Id { get; set; }
    public string Key { get; set; }
    public string Value { get; set; }
}
public class Category
{
    public int CategoryId { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Reference> References { get; set; }
}   

Not only is the related entries not updated, a main property like "Headline" cant be updated either. What am I doing wrong? Create/delete works fine btw.

Was it helpful?

Solution 2

Heres what I was looking for:

TryUpdateModel(reference, "");

It has a shitload of overloads. This works though

OTHER TIPS

As Gert Arnold says, you're not actually modifying any of the Reference values so nothing will be updated. By calling db.Entry(reference).State = EntityState.Modified you're just setting the retrieved entity's state to modified in the ChangeTracker. When you call SaveChanges() it's just going to update the Reference record in the database with the same values that you fetched.

You need to update some of the Reference instance properties to see a change.

[HttpPost]
    public ActionResult Edit(int id, IEnumerable<HttpPostedFileBase> files)
    {
        if (ModelState.IsValid)
        {

            Reference reference = db.References.Single(x => x.Id == id);
            reference.HeaderText = "Changed";
            /* No need to interact with the change tracker as the entity is already tracked and you've made a change */
            // db.Entry(reference).State = EntityState.Modified;


            /* Create/Modify/Update/Delete other entities */

            db.SaveChanges();
            //Other stuff regarding files/images

            return RedirectToAction("Index");
        }
        return View();
     }  
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top