Click here to Skip to main content
15,861,168 members
Articles / Web Development / ASP.NET

Implementing ASP.NET MVC Views in three different ways

Rate me:
Please Sign up or sign in to vote.
4.86/5 (18 votes)
24 May 2012CPOL9 min read 198.3K   2.9K   63   13
Three different approaches for implementing the View role in an MVC application.

Introduction

The ASP.NET MVC Framework allows building a Web application as a composition of three roles: Model, View, and Controller. The View role can be implemented in several ways. The popular view engines used in the ASP.NET MVC 3 Framework are the Razor View Engine and the Web Forms (ASPX) view engine. Other than that a View can be implemented by accessing Controller methods using jQuery. This article illustrates all these ways of implementation of Views in an MVC application.

Creating the MVC application

First let’s create an MVC application and develop the Model and Controller parts. Create an empty MVC3 application. Set the “View engine” as ASPX.

Image 1

A project with the following folder structure will be created:

Image 2

Creating the Model part

In order to create the Model, add the following class under the “Model” folder:

Product.cs
C#
public class Product
{
    public string ProductID { get; set; }
    public string ProductName { get; set; }
    public int Quantity { get; set; }
    public decimal Price { get; set; }
}

Implementing the Controller Method

In order to create a Controller method, right click on the “Controller” folder, then select Add - >Controller. Name the Controller as ProductController.

Image 3

The ProductController contains an action method Index as follows:

C#
public class ProductController : Controller
{
    //
    // GET: /Product/

    public ActionResult Index()
    {
        return View();
    }
}

The method returns an ActionResult. Every method in a controller class returns an ActionResult by default. An ActionResult is a return type of a controller method, also called an action method. ActionResult encapsulates the result of an action method and is used to perform a framework level operation on behalf of the action method.

Let’s modify the action method in order to return a list of Products using the Product model:

C#
public ActionResult ProductList()
{
    List<Product> productLst = new List<Product>{
        new Product{ProductID="P01",ProductName="Pen",Quantity=10,Price=12},
        new Product{ProductID="P02",ProductName="Copy",Quantity=12,Price=20},
        new Product{ProductID="P03",ProductName="Pencil",Quantity=15,Price=22},
        new Product{ProductID="P04",ProductName="Eraser",Quantity=20,Price=27}
                                                       };
    return View();
}

Once we are done with the Controller implementation, we are ready for routing the result to some view interface. Before that we need to understand the routing construct of the MVC Framework.

Action methods, action results, and routing

Action methods and the routing system work together in every MVC application. MVC does this by using a set of conventions, defined as routing patterns, in the global.asax.cs file. These patterns connect HTTP requests with controllers and action methods. As an HTTP Request enters the routing system, it determines which controller should serve data to which view (if any).

global.asax.cs defines the default routing pattern, {controller}/{action}/{id}, as follows:

C#
routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters

In our example, the controller name is “Product” and the action name is “ProductList”. Our controller or action method doesn’t have any parameters. So the URL for routing the result should be “Product/ProductList”.

Approach 1 - Implementing a View using the ASPX engine

As we are done with the Model and Controller implementation, now it’s time to implement the View. Build the project. Right click in the Controller method and select “Add View”:

Image 4

Let the View name be “ProductView”. Check “Create a strongly-typed view”. Select the model as Product from the View data class dropdown. Don’t forget to build the project before adding the View. Otherwise the View data classes would not be populated.

Select “View Engine” as ASPX(C#). Select “View Content” as “List”.

Image 5

Through this screen, the ASP.NET MVC Framework helps us to implement the classic MVC design pattern in our application. We are passing the model as the “View data class” for the relevant view. Besides, we are getting the scope to selecting a “View Engine” in order to render the result returned by the Controller method. In addition to that we can mention the representation structure for the View (List, Detail, etc.) in the “View content” section.

Click “Add” in order to add the View.

A new folder will be created with the controller name (Product), and the View file (“ProductView.aspx”) will be added in the same.

Image 6

Modify the controller method in order to serve data to the newly created View “ProductView.aspx”.

C#
public ActionResult ProductList()
{
    List<Product> productLst = new List<Product>{
      new Product{ProductID="P01",ProductName="Pen",Quantity=10,Price=12},
      new Product{ProductID="P02",ProductName="Copy",Quantity=12,Price=20},
      new Product{ProductID="P03",ProductName="Pencil",Quantity=15,Price=22},
      new Product{ProductID="P04",ProductName="Eraser",Quantity=20,Price=27}
                                               };
    return View("ProductView", productLst);
}

Open the View file “ProductView.aspx”. As we have given the responsibility for rendering View to the ASPX engine, the layout has been developed using ASP.NET’s existing .ASPX markup syntax, using <%= %> blocks:

Image 7

Now that we are done with the View implementation, it’s time to develop an anchor page and access the View through its routing logic. Create an “aspx” page named “default.aspx” under root and access the View through it. This page needs to be handled in a different way than normal ASPX pages. In order to achieve MVC Framework behavior, the page is required to be handled by a special HttpHandler MvcHttpHandler.

Add the following code in the load event of the page:

C#
public partial class _Default : Page
{
    public void Page_Load(object sender, System.EventArgs e)
    {
        HttpContext.Current.RewritePath(Request.ApplicationPath);
        IHttpHandler httpHandler = new MvcHttpHandler();
        httpHandler.ProcessRequest(HttpContext.Current);
    }
}

As per the routing pattern defined in Global.asax ("{controller}/{action}/{id}"), the routing URL for the Product view page would be “Product/ProductList”.

Add a link in the default.aspx page  in order to access the View using its routing construct as follows:

Also inherit the page from the “System.Web.Mvc.ViewPage” namespace.

Image 8

Set “default.aspx” as the startup page and run the application.

Image 9

Click the link in order to view the product list rendered by the ASPX View engine.

Image 10

Approach 2 - Implementing a View using the Razor engine

In the above section, we implemented the View role of our MVC application using the ASPX View engine. Now we will implement the View role of the application using the Razor View engine.

Let’s create one more model Supplier:

Supplier.cs
C#
public class Supplier
{
   public string SupplierID { get; set; } 
   public string SupplierName { get; set; }
   public string SupplierAddress { get; set; }
}

In order to create a Controller method, right click on the “Controller” folder, then select Add - >Controller. Name the controller as SupplierController.

Let’s modify the action method in order to return a list of Suppliers using the Supplier model:

C#
public ActionResult SupplierList()
{
    List<Supplier> supplierList = new List<Supplier>{
     new Supplier{SupplierID="S01", SupplierName="Kausik",SupplierAddress="Kolkata"},
     new Supplier{SupplierID="S02", SupplierName="Sandeep",SupplierAddress="Delhi"},
     new Supplier{SupplierID="S03", SupplierName="Soumick",SupplierAddress="Chennai"},
     new Supplier{SupplierID="S04", SupplierName="Arindam",SupplierAddress="Mumbai"}
                                                       };
    return View();
}

As we are done with the Model and Controller implementation, now it’s time to implement the View. Build the project. Right click in the Controller method and select “Add View”:

Let the View name be “SupplierView”. Check “Create a strongly-typed view”. Select the model as Supplier from the View data class dropdown.

Select “View Engine” as Razor(CSHTML). Select “View Content” as “List”.      

Image 11

Here we are getting the scope to select a different “View Engine” other than ASPX in order to render the result returned by the Controller method. As we have chosen the Razor(CSHTML) engine, our View will be handled by the Razor View engine.

Click “Add” in order to add the View.

A new folder will be created with the controller name (Supplier) and the View file (“SupplierView.cshtml”) will be added in the same. The View page is no more an .aspx page but a .cshtml page.

Image 12

Modify the controller method in order to serve data to the newly created View “SupplierView.aspx”.

C#
public ActionResult SupplierList()
{
   List<Supplier> supplierList = new List<Supplier>{
    new Supplier{SupplierID="S01", SupplierName="Kausik",SupplierAddress="Kolkata"},
    new Supplier{SupplierID="S02", SupplierName="Sandeep",SupplierAddress="Delhi"},
    new Supplier{SupplierID="S03", SupplierName="Soumick",SupplierAddress="Chennai"},
    new Supplier{SupplierID="S04", SupplierName="Arindam",SupplierAddress="Mumbai"}
                                                   };
   return View("SupplierView", supplierList);
}

Open the View file “SupplierView.cshtml”. As we have given the responsibility for rendering the View to the Razor engine, the layout has been developed using Razor’s markup syntax, using a @ character. Unlike <% %> code nuggets, Razor does not require you to explicitly close the code-block:

Image 13

As per the routing pattern defined in Global.asax ("{controller}/{action}/{id}") the routing URL for the Suppliers view page would be “Supplier/SupplierList”.s

Add a link in the default.aspx page in order to access the view using its routing construct as follows:

Image 14

Set the “default.aspx” as the startup page and run the application.

Image 15

Click the link in order to view the suppliers list rendered by the Razor View engine.

Image 16

Approach 3 - Implementing a View using jQuery

In this section, we will implement the View role using jQuery. The View will be handled by the ASPX engine, but we will not be using <% %> code nuggets for manipulating the representation. Rather, we will access the Controller method using a jQuery/Ajax call and manipulate the values returned by the Controller method in the front-end.

Let’s create one more model Order:

Order.cs
C#
public class Order
{
   public string OrderID { get; set; }
   public int OrderQty { get; set; }
   public decimal OrderPrice { get; set; }
}

In order to create a Controller method, right click on the “Controller” folder, then select Add - >Controller. Name the controller as OrderController. Add a new action method GetOrderList in the controller.

The method will return a list of orders using the Order model. In this case we will wrap the returned result into JSON format. Hence the return type of the Controller method is JsonResult. Also the Suppliers list object is being passed into the Framework method Json. The method serializes the list object into JSON format.

C#
public JsonResult GetOrderList()
{
    List<Order> OrderLst = new List<Order>{
        new Order{ OrderID="101", OrderQty=10, OrderPrice=12}, 
        new Order{ OrderID="201", OrderQty=12, OrderPrice=15},
        new Order{ OrderID="301", OrderQty=15, OrderPrice=17}
                                                  };
    return Json(OrderLst, JsonRequestBehavior.AllowGet);
}

Right click in the Controller method and select “Add View”. Set the View name as “OrderView”. Un-check “Create a strongly-typed view”.

Select “View Engine” as ASPX(C#). Set “View Content” as “Empty”.

Image 17

In the earlier implementations, we had passed a specific model to the View and made our View strongly typed. But here we will access the Controller method using an AJAX call from jQuery. So we are not required to pass any model.

Also, in the earlier implementations we had selected “View content” as “List”. Actually, we had selected a representation format and allowed ASPX or the Razor View engine to manipulate the presentation layer with their native syntax (<% %> or @). But here we will implement the presentation layer manually using simple HTML.

A new folder will be created with the controller name (Order), and the View file (“OrderView.aspx”) will be added in the same.

Modify the controller method in order to serve data to the newly created View “OrderView.aspx”.

C#
public ActionResult OrderList()
{
    return View("OrderView");
}

Now, in order to call the controller method GetOrderList, we need to initiate an AJAX call from the “OrderView.aspx” page. We will implement the AJAX call using jQuery. First include the jQuery library. The “Script” folder in the project consists of the jQuery library. Include the path of the “Script” folder in order to get access to the jQuery library:

JavaScript
<script type="text/javascript" src="../../Scripts/jquery-1.4.1.js">

The Controller method GetOrderList will return the list of orders in JSON format. We are required to parse the values from JSON and represent it using an HTML table. For this we need to write a function using jQuery. Let’s write a function CreateHTMLTableFromJSON in a separate script file JQScript.js under the “Script” folder. Also, include the script file in the “OrderView.aspx” page.

XML
<script type="text/javascript" src="../../Scripts/JQScript.js">
</script>

Now initiate the AJAX call as follows:

Image 18

In the above code the Controller method GetOrderList is being called by the jQuery AJAX call. The JSON response returned by the method is being parsed to an HTML table, by the client side function CreateHTMLTableFromJSON written in jQuery. At the end, the HTML table is appended to a “div” named “OrderGrid”.

As per the routing pattern defined in Global.asax ("{controller}/{action}/{id}"), the routing URL for the Product view page would be “Order/OrderList”.

The default.aspx page should have a link to access the View using its routing construct as follows:

Image 19

Set “default.aspx” as the startup page and run the application.

Click the link in order to view the orders list fetched using jQuery:

Image 20

Sometimes in Internet Explorer, a few consecutive refreshes are required for getting results from the jQuery AJAX call. In this case, please try Firefox.

I have tried to explore three different approaches for implementing the View role in an MVC application. A specific approach between these should be adopted in order to develop a robust View in the application.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Pratik Bhuva27-Jan-14 21:37
professionalPratik Bhuva27-Jan-14 21:37 
QuestionI am getting an error Pin
Abhishek_Kashyap14-Feb-13 3:30
Abhishek_Kashyap14-Feb-13 3:30 
AnswerRe: I am getting an error-solved Pin
Ganesh PMP31-Mar-15 19:24
Ganesh PMP31-Mar-15 19:24 
QuestionThanks Pin
Praveen Kumar Prajapati17-Nov-12 2:32
professionalPraveen Kumar Prajapati17-Nov-12 2:32 
Questionsuperbbb ultimate. the most simplest one..... tooo gooodddd aticle Pin
S V Saichandra29-Oct-12 8:08
professionalS V Saichandra29-Oct-12 8:08 
GeneralMy vote of 5 Pin
S V Saichandra13-Sep-12 1:51
professionalS V Saichandra13-Sep-12 1:51 
good one
GeneralNeed more details Pin
ojanacek15-Aug-12 4:14
ojanacek15-Aug-12 4:14 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey6-Jul-12 23:33
professionalManoj Kumar Choubey6-Jul-12 23:33 
SuggestionWhy not use the WebGrid? Pin
MiguelCouto12-Jun-12 22:27
MiguelCouto12-Jun-12 22:27 
GeneralMy vote of 5 Pin
Anurag Gandhi30-May-12 0:46
professionalAnurag Gandhi30-May-12 0:46 
GeneralMy vote of 5 Pin
santosh poojari29-May-12 19:45
santosh poojari29-May-12 19:45 
GeneralMy vote of 4 Pin
cjb11024-May-12 20:50
cjb11024-May-12 20:50 
SuggestionI Recommend PNG Images Pin
AspDotNetDev24-May-12 13:07
protectorAspDotNetDev24-May-12 13:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.