Introduction To Model, View, Controller And Types Of Controller Action Methods

Hey Friends. I hope you are doing well. In today's lession we are going to learn different types of actions methods in the controller part in ASP .NET MVC. These are the basic things that you should know if you are passionate about learning & developing applications using Model, View, Controller architecture. First of all, you need to know the structure of MVC (Model, View, Controller). If you already know how Model, View and Controller relate to each other that will be an advantage.

The following is the list of topics that we will cover today. As a beginner, I recommend that you start with the basics of C# programming. Don't forget to practice OOP concepts.

  1. What is Model in MVC?
  2. What is View in MVC?
  3. What is Controller in MVC and the types of action methods used in MVC controller classes?

Introduction To Model View Controller and types of Controller Action Methods

What is model in MVC

In simple words we can understand that Model contains business data which is being used to get & set the data using entity framework. The model part includes things like data definition, data validation, data access, etc. The controller logic will decide which View(data) part to be displayed in the web page. We now understand that model/business data is the most significant part of the application's business data definition and security as well.

What is view in MVC 

We know the functions of Model(data) definitions. We now understand more about the view part of MVC. The view part which is displayed in the application browser is owned & controlled by the controller, which has single or multiple action methods which return the view part. The View part represents the layout for displaying the datatable result in the browser. It receives the data from the controller business logic which is defined under Model class and prepares it to return the result. Additionally, we can identify the controller and view by looking at the URL.

Introduction to Model, View, Controller and Types of Controller Action Method in ASP.NET MVC Core

Controller in MVC

The controller is responsible for managing and updating the view in response to user requests. This is the main part that contains business logic and controls the request for data drive. Controller decides which response is sent back to the user when browser requests. There are number of methods in the controller base class that we are going to discuss in the next question.

Types of action methods used in controller class

Default action method

The controller class has a default action method called index(), which redirects to the home page of the particular action method. That is configured by default. However we can change the default route thread defined in the Statup.cs class under project, which you can see at the bottom of the project directory. The following example illustrates the route defined by default in the Statup.cs class in core MVC. 

app.UseMvc(Route => {
    routes.MapRoute(name: "Default"
        Template: = "{Controller=Home}/{Action=Index}/{id?}");
    // Or you can define/modify/add multiple routes according to your apps requirements
    routes.MapRoute(name: "Yourname"
        Template: = "{Controller=YourHome}/{Action=YourIndex}/{id?}");
});

Controller class contains various action result method classes which can be assigned as view action results under a single controller. We can define return functions based on the business logic datatable view. There are numbers of action methods which are being used for instance IActionResult, ActionResult, JsonResult, PartialView etc.

These are the types of action result methods that are commonly used in controller classes.

  1. ActionResult
  2. IActionResult
  3. EmptyResult
  4. ContentResult 
  5. PartialViewResult
  6. FileStreamResult
  7. ViewResult
  8. RedirectResult 
  9. FileResult
  10. AuthorizedResult

Note
My next post will cover the foreach type action methods used in controller classes.

Thanks, I hope you understood this explanation.