4

I am trying to learn MVC & EF to move away from WebForms and ADO.NET. I am just throwing together my first trial site so see how it goes and have hit a stumbling block.

I am editing a record on the page and pressing save.I get no errors returned however the data is not updated.

The Article Model being updated

    public class Article
{
    [Key]
    public int Id { get; set; }

    public string Author { get; set; }
    public string Title { get; set; }
    public DateTime DateCreated { get; set; }
    public string Body { get; set; }
    public int Likes { get; set; }
    public int Dislikes { get; set; }
    public List<Comment> Comments { get; set; }
    public string Tags { get; set; }
    public int Category { get; set; }
}

The edit code on the Controller, the articleId is from the querystring.

    [HttpPost]
    public ActionResult Edit(int articleId, FormCollection collection)
    {
        var result = from i in db.Articles
                     where i.Id == articleId
                     select i;

        if (TryUpdateModel(result))
        {
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(result.First());

    }

On debugging, the TryUpdateModel() returns true and calls the db.SaveChanges. No errors are returned. When being directed back to the Index method on the Controller, the article is showing unchanged.

Is it something glaringly obvious?

Many Thanks

Lotok
  • 4,517
  • 1
  • 34
  • 44
  • 1
    first step would be to verify in debugger that the object within the `if` block is actually updated with the new values. – Knaģis Mar 12 '13 at 11:24
  • My understanding was the result passed to TryUpdateModel is the record you are trying to update. The method tries to create a model from the posted data and then tries to update the passed in record. The FormCollection shows the correct model schema but at debug time it does not show values. – Lotok Mar 12 '13 at 11:30
  • Just off the top of my head, during debugging, check that the `result` entity has actually been modified and is being tracked by the DbContext. Look for `EntityState.Modified`. Related SO question: http://stackoverflow.com/questions/7106211/entity-framework-why-explicitly-set-entity-state-to-modified – Sameer Singh Mar 12 '13 at 11:37
  • I am doing reading to see where the DbEntityEntry object fits in. The result object does not seem to have the changes once the breakpoint goes to db.SaveChanges(). I don't see the EntityState property on the result though. – Lotok Mar 12 '13 at 11:55
  • Figured it out. Something really Daft :( – Lotok Mar 12 '13 at 12:02

1 Answers1

0

I had forgotton to select the model out of the enumerable. Adding .First() to select the record fixed it. Just one of those occasions where I couldn't see the wood from the trees!

    [HttpPost]
    public ActionResult Edit(int articleId, FormCollection collection)
    {
        var result = from i in db.Articles
                     where i.Id == articleId
                     select i;

        if (TryUpdateModel(result.First()))
        {

            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(result.First());

    }
Lotok
  • 4,517
  • 1
  • 34
  • 44
  • 2
    I'd suggest altering that LINQ query to be a little clear, something like `var result = db.Articles.Single(i => i.Id == articleId)`. If you select with `Single()`, you won't need to use `First()`. – Graham Mar 12 '13 at 12:24
  • Totally agree. I am guilty of going for more verbose syntax sometimes purely out of (bad) habit. – Lotok Mar 12 '13 at 12:30
  • Updated to expression syntax and moved it over to a method on the DbContext Model for good measure. – Lotok Mar 12 '13 at 12:45