I am a newbie in ASP.NET MVC and learning from zero now by reading the tutorial given in asp.net. My question might be too simple but I haven’t found the answer. For the quick response, I ask here.
Edit action method:
// GET: /Movie/Edit/5
public ActionResult Edit(int id = 0)
{
Movie movie = db.Movies.Find(id);
if (movie == null)
{
return HttpNotFound();
}
return View(movie);
}
//
// POST: /Movie/Edit/5
[HttpPost]
public ActionResult Edit(Movie movie)
{
if (ModelState.IsValid)
{
db.Entry(movie).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(movie);
}
Delete action method:
//
// GET: /Movie/Delete/5
public ActionResult Delete(int id = 0)
{
Movie movie = db.Movies.Find(id);
if (movie == null)
{
return HttpNotFound();
}
return View(movie);
}
//
// POST: /Movie/Delete/5
[HttpPost, ActionName("Delete")]
public ActionResult DeleteConfirmed(int id)
{
Movie movie = db.Movies.Find(id);
db.Movies.Remove(movie);
db.SaveChanges();
return RedirectToAction("Index");
}
Let us compare the HTTP POST for update and deletion. I am curious:
Why does the action method DeleteConfirmed use model id rather than model object as its parameter?
Because all you need in order to delete an entity is its id whereas in order to edit this entity you need the entire object. Also I guess that the view that is calling this Delete action is only sending the id of the entity to delete in the request so it would be meaningless to have the delete action take the whole entity as it will never be bound.