Thursday 19 November 2015

How to create View in ASP.net MVC


                 In this article I'm going to explain how to create a View and how to return the ActionResult View from Controller with small example. This article will help those who are beginners to ASP.net MVC.

 

How to create and call View in ASP.net MVC:

 

Summary:


           In this article I'm going to explain how to create a View and how to return the ActionResult View from Controller with small example. This article will help those who are beginners to ASP.net MVC.

Follow below steps to accomplish the task.


Step 1 : Create View



Select Views in solution explorer then right click on Views and then Add new View.
 

Step 2: 


In Add View dialog box give a name of View and then uncheck the use a layout checkbox and then click on Add button.
 
 
It will add a new View inside "Views/Demo" folder in solution explorer. Views are associated with the particular Controller is placed inside a special folder. This special folder is called as a ControllerName, What are all the Views are a available inside that folder is belongs to against that particular Controller only.

 

 

 

Step 3: Add a content to View




Open DemoView.cshtml and add content to that as like below.
 
 

 @{  
   Layout = null;  
 }  
 <!DOCTYPE html>  
 <html>  
 <head>  
   <meta name="viewport" content="width=device-width" />  
   <title>DemoView</title>  
 </head>  
 <body>  
   <div>   
     Welcome to ASP.net MVC 5  
   </div>  
 </body>  
 </html>  
 
 

Step 4: Create New Action method




Add a new Action method inside DemoController as follows.
 
 
 public class DemoController : Controller  
 {  
     //  
     // GET: /Demo/  
     public ActionResult Index()  
     {  
       return View();  
     }  
     public ActionResult GetView()  
     {  
       return View("DemoView");  
     }  
 }  
 

Result:

 
And run the application like below format.
 

No comments:

Post a Comment