Using Extension Methods in MVC 4.0 Applications

In this article we are going to focus on how to use Extension Methods in MVC 4.0 Application. Extension Methods are powerful in providing the custom add on functionality for the user defined types. We will calculate TotalPrice using extension method.

In this article we are going to focus on how to use Extension Methods in MVC 4.0 Application. Extension Methods are powerful in providing the custom add on functionality for the types.

Step 1: Launch Visual Studio and Create New Project. Then Select Visual C# under Templates -> ASP.NET MVC 4 Web Application ->Give the name: ExtensionMethodDemo ->click OK
Step 2: Select Empty Project -> click on OK.
Step 3: Go to View menu -> Solution Explorer -> Right Click Models folder -> Add -> New Item -> Class -> Provide the class name as "Item".
Step 4: Add the following code to the Item.cs file.

public class Item
public int ItemId { get; set; }
private string Name { get; set; }
public decimal Price { get; set; }
public string Category { set; get; }

Step 5: Right click Controllers folders -> Add -> Controller -> Give the name as "HomeController". Click OK
Step 6: Add a static class "MyExtensionMethodDemo" in the Models folder as shown below.

public static class MyExtensionMethodDemo
public static decimal TotalPrice(this IEnumerable<Item> item)
decimal price = 0;
foreach(Item itm in item)
price += itm.Price;
return price;

Here we are adding an extension method TotalPrice to calculate the total price of the items sent as parameter to the TotalPrice method by adding the price of each item in the collection.
Step 6: Using the extension method "TotalPrice" in Controller class: Add the below method in the Controller class

public ViewResult GetTotalPrice()
List<Item> lstItem = new List<Item>()
new Item(){ ItemId=1, Price=100, Category="ItemA"},
new Item(){ ItemId=1, Price=100, Category="ItemB"},
new Item(){ ItemId=1, Price=100, Category="ItemC"}

return View("Result",(object)String.Format("Total Price: {0}",lstItem.TotalPrice().ToString()));

In this method we are adding some Items to the List of Items and then calling the TotalPrice extension method of the List.Please note that we are converting the second parameter to object type because if we dont convert it then it is assumed to be the name of a masterpage as it is another overload of the View. As you can see in the below screen shot, the TotalPrice extension method is displayed in the intellisense as if the method is part of the Items collection.
Step 7:Generating the View
Right Click the Index method and select Add View.
Provide the View name as Result and uncheck "Use a layout or masterpage" as we are not using master page in our project.
This creates a .cshtml file with the name "Result" add "@model string" statement as the first line in the Result.cshtml file. Here we are saying that the model is of type string which our Result view will use.
Step 8:Displaying the result
In the Result.cshtml file, under the div tag add @Model which will display the string value returned by our Action Method in the controller class.
The Result.cshtml file has the below content.

@model string
Layout = null;

<!DOCTYPE html>

<meta name="viewport" content="width=device-width" />

Now Build the project and Press Ctrl +F5. Now in the browser add the following after the port number
The url will be something like below. the port number may vary

This will display the result as shown below:
Total Price: 300


No responses found. Be the first to comment...

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: