У меня есть метод действия для обработки HTTP-POST следующим образом.
[HttpPost]
public ActionResult Edit(Movie model)
{
var movie = db.Movies.FirstOrDefault(x => x.Id == model.Id);
if (movie == null)
{
TempData["MESSAGE"] = "No movie with id = " + id + ".";
return RedirectToAction("Index", "Home");
}
if (!ModelState.IsValid)
return View(model);
// what method do I have to invoke here
// to update the movie object based on the model parameter?
db.SaveChanges();
return RedirectToAction("Index");
}
Вопрос: Как обновить movie
на основе model
?
Редактировать 1
На основе решения @ lukled приведен окончательный и рабочий код:
[HttpPost]
public ActionResult Edit(Movie model)
{
var movie = db.Movies.FirstOrDefault(x => x.Id == model.Id);
if (movie == null)
{
TempData["MESSAGE"] = string.Format("There is no Movie with id = {0}.", movie.Id);
return RedirectToAction("Index", "Home");
}
if (!ModelState.IsValid)
return View(model);
var entry = db.Entry(movie);
entry.CurrentValues.SetValues(model);
db.SaveChanges();
return RedirectToAction("Index");
}