Creating View in MVC



In this tutorial you will learn how to create Views in MVC using different ways.
View- It is a simple html file which we use to show data to the end user. Using view user can post and get data from server. HTML understands by browser(Chrome,IE.etc..).
(1)Using Add view Menu options in solution explorer.
(2)Creating view using controller class.
 Now let’s start with first one.
 (1)Using Add view Menu options in solution explorer-
 Step 1: Create Folder with name demo inside Views folder-

Step 2: After that you need right click on Demo folder and then select Add – View option from menu.

Step 3: Now you will see below popup window. Here you just need to provide View name and then click on Add button.

Step 4: Now you can see Test.csHtml page appear with some default code. In this step we have created View.

 Step 5: We have done with view creation. But individual view is nothing in MVC. In MVC we also need to know how to use above created(Test.cshtml) view in controller OR how to call View from Controller.
Let’s start calling View from controller class.
For this we need to create controller class. For Creating controller class click here CreatingController
I assume you have created controller class now I will show you how you can call existing views.

public ActionResult Index()
        {
            //Calling View like this
            return View("Test");
        }

After that you can see Test page in browser.
Now we have completed one point. Let’s go for next point.

 (2)Creating view using controller class.
Now we are here to learn how we can create controller using controller class. If you have idea about controller class then my suggestion please go through this tutorial CreatingController. Then come to this point. In this point I will cover only how you can create view using controller class.
Step 1:For creating view you need to just right click on any function name in controller class –then click on Add View menu.

 Step 2: After that you will see below popup window appear. Here you can see default name of view is function name but you can change as per requirement and then proceed for Add button. In this window you can see many other options are available like Template, Create as a partial view, Use a layout page. All these option I will explain in detail in next article. Here we will learn how simply we can create view.

 In my case I have changed name of view and then click Add.

 After click on Add you can see your view content like below image.

 Step 3: Now time to calling view from controller class. Calling view is same as you can see in previous point.

Finally you can see your view in browser.

I hope you are enjoying this article. Please  leave your comment in comment box for any query.Thanks
Author

Priti Kumari

I am technical blogger.I blogs at www.c-Sharpcorner.com and https://aspdotnetmyblog.blogspot.in/.

No comments:

Post a Comment