我有一个操作方法来处理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");
}
问题:如何在模型
的基础上更新电影
?
编辑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");
}