views:

74

answers:

2

I'm using an Edit View in ASP.NET MVC and after an edit it returns the Edited Entity back. What's the best way to get the edited values back to the databse.

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Edit(int id, Movies EditedMovie)
    {
        var orginalMovie = _db_linq.Movies.First(e => e.Id == id);
        if (!ModelState.IsValid)
            return View(orginalMovie);

        _db_linq.Movies.Attach(EditedMovie, true);
        _db_linq.SubmitChanges();

        return RedirectToAction("Index");
    }

Returns the Error "Cannot add an entity with a key that is already in use."

orginalMovie = EditedMovie; doesn't work,too I have too copy each property seperate, i.e.

orginalMovie.Name = EditedMovie.Name;
+2  A: 

For Entity Framework:

Change:

    _db_linq.Movies.Attach(EditedMovie, true);

to:

    _db_linq.Movies.ApplyPropertyChanges(EditedMovie);
aleemb
thanks. but this method doesn't exist.
Henrik P. Hessel
Are you using Linq to SQL or Entity Framework? This approach is for Entity Framework.
aleemb
Linq to SQL Class
Henrik P. Hessel
private MovieApp.Models.MovieDBEntities _db = new MovieApp.Models.MovieDBEntities();
Henrik P. Hessel
Okay, thanks. I got the error.
Henrik P. Hessel