In The Name of Allah.
Outlines:
Action Method Parameters
Action selectors
HTTP Methods
Model
View
Action Method Parameters
Every action methods can have input parameters as normal methods.
It can be primitive data type or complex type parameters.
Action selectors
Action selector is the attribute that can be applied to the action methods.
It helps routing engine to select the correct action method to handle a
particular request.
MVC 5 includes the following action selector attributes:
1. ActionName
2. NonAction
3. ActionVerbs
ActionName
ActionName attribute allows us to specify a different action name than the
method name.
NonAction
NonAction selector attribute indicates that a public method of a Controller
is not an action method.
Use NonAction attribute when you want public method in a controller but
do not want to treat it as an action method.
ActionVerbs
The ActionVerbs selector is used when you want to control the selection of
an action method based on a Http request method.
For example, you can define two different action methods with the same
name but one action method responds to an HTTP Get request and
another action method responds to an HTTP Post request.
MVC framework supports different ActionVerbs, such as HttpGet, HttpPost,
HttpPut, HttpDelete, HttpOptions & HttpPatch.
You can apply these attributes to action method to indicate the kind of
Http request the action method supports.
If you do not apply any attribute then it considers it a GET request by
default.
ActionVerbs
HTTP Methods
Different action methods supports different ActionVerbs
Multiple HTTP verbs using AcceptVerbs
You can also apply multiple http verbs using AcceptVerbs attribute.
GetAndPostAction method supports both, GET and POST
ActionVerbs in the following example:
ASP.NET MVC - Model
Model represents domain specific data and business logic in MVC
architecture.
It maintains the data of the application.
Model objects retrieve and store model state in the persistence store like a
database.
Model class holds data in public properties.
All the Model classes reside in the Model folder in MVC folder structure.
Adding a Model
Create a project then right click on Model folder->Add->Click on Class
Adding a Model
This will add new Student class in model folder. Now, add Id, Name, Age
properties as shown below
ASP.NET MVC - View
View is a user interface.
View displays data from the model to the user and also enables them to
modify the data.
ASP.NET MVC views are stored in Views folder.
Different action methods of a single controller class can render different
views, so the Views folder contains a separate folder for each controller
with the same name as controller, in order to accommodate multiple
views.
For example, views, which will be rendered from any of the action
methods of HomeController, resides in Views > Home folder.
In the same way, views which will be rendered from StudentController,
will resides in Views > Student folder.
ASP.NET MVC - View
Create New View
We have already created StudentController and Student model in the previous
section.
Now, let's create a Student view and understand how to use model into view.
We will create a view, which will be rendered from Index method of
StudentContoller. So, open a StudentController class -> right click inside Index
method -> click Add View
Create New View
Add View dialogue box
In the Add View dialogue box, keep the view name as Index.
It's good practice to keep the view name the same as the action
method name so that you don't have to specify view name explicitly in the
action method while returning the view.
Add View dialogue box
THE END!