Adding Controller, Model and View in ASP.NET MVC



In MVC architecture, the Controller plays a very important role. So in this article, we will learn about how to add controller, Model, and View in ASP.NET MVC.

Let’s see how to add a controller in MVC.





See in the solution explorer, there is folder structure in MVC. For adding Controller, Right-click on the Controllers folder 

Controllers Folder -> Add -> Controller


After Click on Controller, New Window is open where we can select which type of controller you have to add means Empty MVC Controller or with CRUD operation MVC controller using Entity Framework or Web API Controller.


You can select an empty controller, so it creates an empty controller without creating any action method. After clicking on the Add button at the bottom of the window, it opens another window where we want to pass the controller name. See in below window.


 
Here we can pass the controller name. While adding controller please don’t remove controller word after our controller name else it can’t find that It is a controller in the routing table.

See after adding controller your controller looks like this. Basically, the controller is inherited from the “Controller” base class.




Adding Model:

Model is a simple real-time entity adding in Models folder for the business logic purpose and data operations performed in the model also. For adding Model we create a simple class file with real-time Entity like below.

public class ResetPasswordViewModel
{
        [Required]
        [EmailAddress]
        [Display(Name = "Email")]
        public string Email { get; set; }

        [Required]
        [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public string Password { get; set; }

        [DataType(DataType.Password)]
        [Display(Name = "Confirm password")]
        [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
        public string ConfirmPassword { get; set; }

        public string Code { get; set; }
}

This is the model for the Reset password view model. Where we have used some properties and business logic for those properties by using data annotations.

Adding View:


Currently, when your application runs at this point, with URL {ControllerName}/{ActionMethod}, it goes to that controller and finds an action method with that name. See in the above image, the Controller name is “ABC” and the action method name is “Index”. So when you hit the URL with “localhost:63961/ABC/Index”. It executes action method. In this action method we have return one statement return view(), this statement used for returning view to the browser. For returning view, it first checks view is present or not. If not present it shows below error:
 


In the above image, you will get an idea about where our view must be present. When we add controller in the controller folder, One another folder is created as a child folder in Views folder with the name of the controller. If you expand the Views folder you will see there is one new folder added with “ABC” name see in the below image.


Now for adding the view, you have to simply right click on Action method name, you will get the option to Add View like below.


After click on Add View option, a new window will open like below where we pass some information about our view like layout for the view, scaffolding options and which model you want to display.


Now for a simple adding view without selecting the scaffolding option click on the Add button. After the click of Add button, in our Controller name folder in the Views, one .cshtml file is added with View Name like below.


Now our view is also ready for use. Now once again you run the application and see what happen, your application is displaying index page like below.



See below link for related articles

1.       Getting Started with MVC


2.      ViewBag, ViewData and TempData


3.       Folder Structure In MVC


4.       MVC Architecture Theory


5.       Data Annotations and Validations in MVC


6.       MVC Architecture: Traditional Routes


7.       MVC Architecture: Attribute-based Routing


8.       Bundling and Minification in MVC


9.       AutoComplete in MVC

                    http://dotnet-root.blogspot.in/2016/10/autocomplete-using-aspnet-mvc-and-jquery.html


10.       Adding Controller, Model, and Views

                      http://dotnet-root.blogspot.in/2016/11/adding-controller-model-and-view-in.html 








Post a Comment

1 Comments

  1. This comment has been removed by a blog administrator.

    ReplyDelete