To edit data requires two Actions in the controller. The first will display the HTML form with the current values displayed to allow them to be edited, the second Action will edit the values submitted from the form. As with the Adding Data example , both of these Actions will have the same name but are differentiated by having attributes of HttpGet for the controller showing the blank form, and HttpPost for performing the edit.
In the Controllers/HomeController add the first action as follows:
This is similiar to the Details Page in that it receives an id parameter via the URL and the routing pattern of localhost:5001/Home/UpdateFilm/2
The UpdateFilm Controller expects to be sent an id value. We'll do this by editing the AllFilms View.
Create a View file of Views/Home/UpdateFilm with the following HTML form:
The above creates the form with the values populated.
In the controller add the second Action for the httpPost action:
The context updates the model via the Update method of DbContext and the SaveChanges method of the DbSet is then used to commit the change to the database. The controller then redirects to the AllFilms Action and thus the AllFilms View.