Click here to Skip to main content
15,883,883 members
Articles / Web Development / HTML
Tip/Trick

Implementing AJAX in ASP.NET MVC

Rate me:
Please Sign up or sign in to vote.
4.84/5 (20 votes)
14 Mar 2015CPOL4 min read 136.6K   3.6K   27   11
This tip explains implementing AJAX in ASP.NET MVC.

Sample Image - maximum width is 600 pixels

Introduction

This tip explains how we can implement AJAX in ASP.NET MVC. Here, I have attempted to explain the concept of partial page updates using a partial view to display product details asynchronously. For simplicity, I have stored details of three products statically in a list. The application searches the product code entered by a user and displays the corresponding product details.

Background

Traditional web applications rely on synchronous method calls to process data. The entire web page is posted to the server each time data is submitted through the page, causing performance delays.

This problem can be overcome by using AJAX. AJAX allows web applications to call methods asynchronously. Instead of posting the entire page to the web server, only the required data is posted. This improves the overall performance of the web applications.

Using the Code

Begin by creating a new ASP.NET MVC4 project as follows:

Image 2

We will use a model class to display data in a partial view. For this, we have to add a Product class in the Models folder as follows by right clicking on the Models folder and selecting the Add Class option:

Image 3

Following is the code of the Product class:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace AJAXDemo.Models
{
    public class Product
    {
        public string ProdCode
        {
            get;
            set;
        }
        public string ProdName
        {
            get;
            set;
        }
        public int ProdQty
        {
            get;
            set;
        }
    }
}

Then, add new layout in the Shared folder under the Views folder as follows:

Image 4

Following is the code of the _Layout.cshtml file:

HTML
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>_Layout</title>
    @RenderSection("scripts")
</head>
<body>
    <div>
        @RenderBody()
    </div>
</body>
</html>

This layout will be used by the Index view. In this layout, the RenderSection() method defines a placeholder for the scripts section and the RenderBody() method defines a placeholder for the main content of the Index view.

Add a new controller as follows by right clicking on the Controllers folder:

Image 5

Following is the code of the Index() method:

C#
public ActionResult Index()
{
   Product p1 = new Product { ProdCode = "P001", ProdName = "Mobile", ProdQty = 75 };
   Product p2 = new Product { ProdCode = "P002", ProdName = "Laptop", ProdQty = 125 };
   Product p3 = new Product { ProdCode = "P003", ProdName = "Netbook", ProdQty = 100 };
   prodList.Add(p1);
   prodList.Add(p2);
   prodList.Add(p3);
   return View();
}

This code creates three product objects and adds them to a static list called prodList which is declared at the class level and it renders the view associated with the Index view.

Add the view corresponding to the above action method as follows by right clicking on the Index() method in the controller:
(Note: Remember to select the layout page while adding the Index view.)

Image 6

Following is the code of the Index.cshtml view:

HTML
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@section scripts
{
    <script type="text/javascript" src="~/Scripts/jquery-1.7.1.js"></script>
    <script type="text/javascript" src="~/Scripts/jquery.unobtrusive-ajax.js"></script>
}

<h2>Index</h2>
@Ajax.BeginForm("ShowDetails","Ajax",
     new AjaxOptions
     { HttpMethod = "POST", UpdateTargetId = "div1", 
     InsertionMode = InsertionMode.Replace, LoadingElementId = "waitimage" })
<h1>Enter Product Code</h1>
<h2>
    Product Code: @Html.TextBox("txtCode")<br />
    <input type="submit" value="Show Details" />
    <input type="reset" value="Clear"/>
</h2>
@{Html.EndForm();}
<br />
<img id="waitimage" src="~/Images/loading42.gif" style="display:none" />
<br />
<div id="div1">
</div>

The above view references the jquery.unobtrusive-ajax.js script to implement the AJAX functionality. It also uses a div with the id "div1" to display the requested product details. It uses the Ajax.BeginForm() helper method to accept product code and submit it asynchronously. The first parameter of the Ajax.BeginForm() method specifies the name of the action method to be invoked to process the input data, the second parameter specifies the name of the controller and the third parameter specifies the AjaxOptions to be used. In the AjaxOptions, the HttpMethod property specifies the HTTP method to be used, the UpdateTargetId property specifies the ID of the div element where the partial view will be displayed, the InsertionMode property specifies the mode of insertion of the partial view, and the LoadingElemetId property specifies the ID of the image to be displayed while waiting for the asynchronous call to be completed and the view to be rendered.

The InsertionMode property can have one of the following values:

  • InsertionMode.InsertAfter - The new details are added after the existing details
  • InsertionMode.InsertBefore - The new details are added before the existing details
  • InsertionMode.Replace - The new details replace the existing details

In order to process the input and search the product code entered by the user, we add a method in the controller which returns a partial view as follows:

C#
public PartialViewResult ShowDetails()
{
   System.Threading.Thread.Sleep(3000);
   string code = Request.Form["txtCode"];
   Product prod = new Product();
   foreach(Product p in prodList)
   {
       if (p.ProdCode == code)
       {
           prod = p;
           break;
       }
   }
   return PartialView("_ShowDetails", prod);
}

The above code searches the entered product code and returns the product corresponding to it using a partial view. It uses the Thread.Sleep() method to simulate delay in fetching the product details. This allows you to view the page loading animation while waiting for the asynchronous method call to complete.

In order to display the product details, we have to add a strongly typed partial view as follows by right clicking on the method and selecting the Add View option:

Image 7

Following is the code of the _ShowDetails view:

HTML
@model AJAXDemo.Models.Product

@if(Model.ProdCode==null)
{
    <h1>Invalid Product Code</h1>
}
else
{
    <h1>Product Details</h1>
    <h2>
        Product Code: @Model.ProdCode<br />
        Product Name: @Model.ProdName<br />
        Product Quantity: @Model.ProdQty<br />
    </h2>
}

The above code displays the product details if the product code is found and the message "Invalid Product Code" if it is not found.

Before executing the application, we have to specify the routing information in the RouteConfig.cs file in the App_Start folder as follows:

HTML
routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Ajax", action = "Index", id = UrlParameter.Optional }
            );

Also, we need to enable unobtrusive JavaScript by adding the "UnobtrusiveJavaScriptEnabled" key under the <configuration> element in the Web.config file as follows:

HTML
<appSettings>
    <add key="webpages:Version" value="2.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="PreserveLoginUrl" value="true" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>

On execution, the application produces the following output:

Image 8

Entering a product code and clicking the submit button displays the corresponding product details asynchronously.

Points of Interest

AJAX is a very important feature of web applications and it provides significant performance advantage over traditional web applications.

I have developed the application using Microsoft Visual Studio Express 2013 for Web.

License

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


Written By
Instructor / Trainer NIIT, India
India India
I am a trainer by profession. Currently I am working with iFuture Technologies(India) as a Senior Faculty. I enjoy programming as a hobby. During my career I have seen the growth and decline of many technologies, many of them being my favorites like Flash, WPF, Windows Mobile Development. Few of my current favorites are Android, Xamarin and Python, though I also like traditional and evergreen languages like PHP, C#, Visual Basic and Java.

Apart from computers, my favorite pastime is bicycling.

Comments and Discussions

 
PraiseExcellent article! Pin
Anatot30-May-21 1:09
Anatot30-May-21 1:09 
QuestionPartial View does not seem to work Pin
charles92219-Jul-17 8:30
charles92219-Jul-17 8:30 
Questionrequest.form return always null Pin
Member 1303984322-Mar-17 20:53
Member 1303984322-Mar-17 20:53 
Questionredirecting problem Pin
IndrajitDasgupat12-Sep-16 23:51
IndrajitDasgupat12-Sep-16 23:51 
PraiseVery useful post, than you!!! Pin
omesur4-Aug-16 9:52
omesur4-Aug-16 9:52 
I´m new to Ajax and this article helps a lot.
Questionabout " prodList " Pin
ILYES0079-Mar-16 4:45
ILYES0079-Mar-16 4:45 
AnswerRe: about " prodList " Pin
ILYES0079-Mar-16 6:15
ILYES0079-Mar-16 6:15 
Questionajax implimentation in mvc Pin
Member 1197039828-Oct-15 1:51
Member 1197039828-Oct-15 1:51 
AnswerRe: ajax implimentation in mvc Pin
AlbertoDC4-Nov-15 1:22
AlbertoDC4-Nov-15 1:22 
GeneralMy vote of 5 Pin
Saleem_beig3-Sep-15 0:47
Saleem_beig3-Sep-15 0:47 
QuestionVery Useful Pin
Pascualito15-Jul-15 11:07
professionalPascualito15-Jul-15 11: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.