Edit & Delete actions
Implementation of Edit and Delete
Let us see the implementation of Edit and Delete functionalities for the movies list, on the same lines. We need to scaffold these like we did it for other action methods:
Contents of Edit.cshtml file
Contents of Delete.cshtml file
Edit action method in the controller (GET)
Edit action method in the controller (POST)
Delete action method in the controller
An important security feature built into the method is that the code verifies that the search method has found a movie before it tries to do anything with it
For example, a hacker could introduce errors into the site by changing the URL created by the links from http://localhost:{PORT}/Movies/Details/1
to something likehttp://localhost:{PORT}/Movies/Details/12345
DeleteConfirmed action method in the controller
Note that the HTTP GET Delete
method doesn't delete the specified movie, it returns a view of the movie where you can submit (HttpPost) the deletion.
Performing a delete operation in response to a GET request (or for that matter, performing any operation that changes data) opens up a security hole!
The ActionName("Delete")
attribute performs mapping for the routing system so that a URL that includes /Delete/ for a POST request will find the DeleteConfirmed
method! Another common work around for methods that have identical names and signatures is to artificially change the signature of the POST method to include an extra (unused) parameter!
Add Update
method stub in FakeContent
Update
method stub in FakeContentThe application should compile and run successfully now; however, it will not be fully functional since we do not have a real data store backing our context!
Notes
We do model validation wherever applicable
We redirect to the index page using the
RedirectToAction
methodWe can do exception handling where applicable, in order to provide a better user experience
Last updated