ASP.Net MVC View

Introduction: In my previous article we have seen: 
  1. How to get version of our MVC application ? 
  2. How to create our own MVC application ?
  3. ASP.NET MVC controller
Now this article explains views and why we use views.
 
View: A view is responsible for displaying all of, or a portion of, data for users. In simple terms, whatever we see on the output screen is a view. Now in the previous article (ASP.NET MVC controller) we have taken the following example:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6.   
  7. namespace DemoMVC.Controllers  
  8. {  
  9.     public class EmployeeController : Controller  
  10.     {  
  11.         public string EmployeeNames()  
  12.         {  
  13.             return @"<ul>  
  14.                         <li>Sourabh Somani</li>  
  15.                         <li>Shaili Dashora</li>  
  16.                         <li>Saloni Choudhry</li>  
  17.                         <li>Mahesh Chand</li>  
  18.                         <li>DJ</li>  
  19.                         <li>Dinesh Beniwal</li>  
  20.                      </ul>";  
  21.         }  
  22.     }  
  23. }  
In this we have a Controller named EmployeeController and an action named EmployeeNames that is returning a string. When we run this file we get the following output:
 
Whatever we have seen in the receding output screen is nothing but a view. You can see that we are rendering a HTML list. Now instead of rendering this list we render a beautiful HTML form.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6.   
  7. namespace DemoMVC.Controllers  
  8. {  
  9.     public class HomeController : Controller  
  10.     {  
  11.         //  
  12.         // GET: /Home/  
  13.   
  14.         public string Index()  
  15.         {  
  16.             string str= @"  
  17. <form>  
  18.    <table>  
  19.       <tr>  
  20.          <td>Name:</td>  
  21.          <td><input type='text' name='Name' id='Name' /></td>  
  22.       </tr>  
  23.   
  24.       <tr>  
  25.          <td>Age:</td>  
  26.          <td><input type='number' name='Age' id='Age' /></td>  
  27.       </tr>  
  28.   
  29.       <tr>  
  30.          <td>Gender:</td>  
  31.          <td>  
  32.             <input type='radio' name='Gender' id='Gender' value='Male'/>Male  
  33.             <input type='radio' name='Gender' id='Gender' value='Female' />Female  
  34.          </td>  
  35.       </tr>  
  36.   
  37.       <tr>  
  38.          <td>Email:</td>  
  39.          <td><input type='email' name='Email' id='Email' placeholder='[email protected]' /></td>  
  40.       </tr>  
  41.   
  42.       <tr>  
  43.          <td>Mobile Number:</td>  
  44.          <td><input type='text' name='Mobile' id='Mobile' /></td>  
  45.       </tr>  
  46.   
  47.       <tr>  
  48.          <td>Qualification</td>  
  49.          <td>  
  50.             <select id='Qualification' name='Qualification'>  
  51.                <option value=''></option>  
  52.                <option value='B.Tech'>B.Tech</option>  
  53.                <option value='M.Tech'>M.Tech</option>  
  54.                <option value='BCA'>BCA</option>  
  55.                <option value='MCA'>MCA</option>  
  56.                <option value='MBA'>MBA</option>  
  57.                <option value='Diploma'>Diploma</option>  
  58.             </select>  
  59.          </td>  
  60.       </tr>  
  61.   
  62.       <tr>  
  63.          <td>Address:</td>  
  64.          <td><textarea cols='40' rows='6' name='Address' id='Address'></textarea></td>  
  65.       </tr>  
  66.   
  67.       <tr>  
  68.          <td colspan='2'>  
  69.             <input type='submit' value='Register' />  
  70.             <input type='reset' />  
  71.          </td>  
  72.      </tr>  
  73.   
  74.    </table>  
  75. </form>  
  76. ";  
  77.             return str;  
  78.         }  
  79.   
  80.     }  
  81. }  
Output:
 
 
In the preceding example we are rendering a HTML form, but this form is on the controller and we are returning a form using a single string. So if we want to manage code above then it will be very difficult for us. So we create a view.
 
The following describes how to create a view:
  1. To add a view right-click on the action (method) and click on Add View.



  2. Now a dialog will be opened, provide the name of the view; by default the name will be the same as the action name.



  3. Now click on the Add button.
  4. After adding a view, inside the view's folder another folder will be created that will be named the same as the controller's name and inside that a file will be created that will be a cshtml. 

  5. Now open the cshtml file. 
In the above example we have created a form in the controller. We will now remove this complete form's code and put it inside the cshtml file or in the view.
 
Index.cshtml 
  1. @{  
  2.     Layout = null;  
  3. }  
  4.   
  5. <!DOCTYPE html>  
  6.   
  7. <html>  
  8. <head>  
  9.     <meta name="viewport" content="width=device-width" />  
  10.     <title>Index</title>  
  11. </head>  
  12. <body>  
  13.     <div>  
  14.         <form>  
  15.             <table>  
  16.                 <tr>  
  17.                     <td>Name:</td>  
  18.                     <td><input type='text' name='Name' id='Name' /></td>  
  19.                 </tr>  
  20.   
  21.                 <tr>  
  22.                     <td>Age:</td>  
  23.                     <td><input type='number' name='Age' id='Age' /></td>  
  24.                 </tr>  
  25.   
  26.                 <tr>  
  27.                     <td>Gender:</td>  
  28.                     <td>  
  29.                         <input type='radio' name='Gender' id='Gender' value='Male' />Male  
  30.                         <input type='radio' name='Gender' id='Gender' value='Female' />Female  
  31.                     </td>  
  32.                 </tr>  
  33.   
  34.                 <tr>  
  35.                     <td>Email:</td>  
  36.                     <td><input type='email' name='Email' id='Email' placeholder='[email protected]' /></td>  
  37.                 </tr>  
  38.   
  39.                 <tr>  
  40.                     <td>Mobile Number:</td>  
  41.                     <td><input type='text' name='Mobile' id='Mobile' /></td>  
  42.                 </tr>  
  43.   
  44.                 <tr>  
  45.                     <td>Qualification</td>  
  46.                     <td>  
  47.                         <select id='Qualification' name='Qualification'>  
  48.                             <option value=''></option>  
  49.                             <option value='B.Tech'>B.Tech</option>  
  50.                             <option value='M.Tech'>M.Tech</option>  
  51.                             <option value='BCA'>BCA</option>  
  52.                             <option value='MCA'>MCA</option>  
  53.                             <option value='MBA'>MBA</option>  
  54.                             <option value='Diploma'>Diploma</option>  
  55.                         </select>  
  56.                     </td>  
  57.                 </tr>  
  58.   
  59.                 <tr>  
  60.                     <td>Address:</td>  
  61.                     <td><textarea cols='60' rows='6' name='Address' id='Address'></textarea></td>  
  62.                 </tr>  
  63.   
  64.                 <tr>  
  65.                     <td colspan='2'>  
  66.                         <input type='submit' value='Register' />  
  67.                         <input type='reset' />  
  68.                     </td>  
  69.                 </tr>  
  70.   
  71.             </table>  
  72.         </form>  
  73.     </div>  
  74. </body>  
  75. </html>  
Now the controller, index action or index method will not return a string anymore, they will return a ViewResult so now in HomeContoller.cs will be:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6.   
  7. namespace DemoMVC.Controllers  
  8. {  
  9.     public class HomeController : Controller  
  10.     {  
  11.         public ViewResult Index()  
  12.         {  
  13.             return View();  
  14.         }  
  15.   
  16.     }  
  17. }  
Output:
 
 
Now after adding the view we can easily manage our form.
 
Here we are returning ViewResult, but we can also return ActionResult
 
Why ActionResult
 
We can return an ActionResult because the ViewResult class is inherited from ViewResultBase and the ViewResultBase class is inherited from ActionResult so we can return an ActionResult also.
 
ActionResult?

A controller's action does not return views only. A controller action returns an ActionResult. An ActionResult is what a controller action returns in response to a browser request.
In ASP.NET MVC a ActionResult can return the following several types (Reference):
 

ViewResult

Represents HTML and markup.

EmptyResult

Represents no result.

RedirectResult

Represents a redirection to a new URL.

JsonResult

Represents a JavaScript Object Notation result that can be used in an AJAX application.

JavaScriptResult

Represents a JavaScript script.

ContentResult

Represents a text result.

FileContentResult

Represents a downloadable file (with the binary content).

FilePathResult

Represents a downloadable file (with a path).

FileStreamResult

Represents a downloadable file (with a file stream).

 
cshtml Files

A .cshtml file is a C# ("C Sharp") HTML file used by a Rezor View Engine. An ASP.Net view engine generates a HTML file for the client. In ASP.NET our complete code is ASP.NET code but Razor reads .cshtml files. In this file we can write C# code as well as HTML code. The HTML code is sent directly to the client and Razor just renders the C# code. On the server side the C# code is compiled and sent to the browser. We will discuss the .cshtml file more in my later MVC article.
 

Up Next
    Ebook Download
    View all
    Learn
    View all
    sourabhsomani.com