The first create action
Last updated
Last updated
Let us now work on implementing the first create action method of the MoviesController, which will present an empty movie form for the user to fill in the movie details so that he can create one entry.
We can use the same technique to scaffold a view, like we did for Index view. But the view name and template need to be as shown below:
Click on Add
The code generated is a shown, we could also do this manually:
That's a lot of markup code! Thanks to scaffolding, we did not have to do it manually!
The code has a standard html form with razor tag helpers for dynamically creating labels, fields and validation elements based on the datatype of the property of the model for which it is being created! OfCourse, we can modify them as required as long as we are not breaking any conventions.
Notice that the datatype the view will be working on, as declared on the top of the code is @model MvcMovie.Models.Movie
and not a collection of movies!
Let us see what we have to do for preparing the first Create action method in the controller. Here is the current code for it:
There would be two Create() methods in the MoviesConrtoller
, one for GET request which just shows a new movie form to the user, the other is for actual creation which we will see next!
This method will be selected by the controller when it get's the request like shown below. It will then return the corresponding view by matching names, which is Create!
No need to modify this since it is just a new movie object that is required to hold the (html from) data that the user will enter in order to create a movie!
Above URL must be working by now, you can actually try running the app! We will see how to process with this next.