How To Use Ajax.BeginForm In ASP.NET MVC 5 App Using Razor View Engine

Step 1

Create MVC app with the name ajaxMVC.

Step 2

Add the folder in the application with the name Images.

Step 3

Inside Images folder, add spinner.gif image file, which we are going to show as loading element during AJAX call

Step 4

In order to use Ajax.BeginForm, we need to install NuGet Package below.

Go to Package Manager console and use the command given below.

PM> Install-Package Microsoft.jQuery.Unobtrusive.Ajax

Below fig.1 demonstrates it


Step 5

Now, in your HomeController, add ShowTime() Method

Now, your Home Controller will look, as shown below. 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Threading;  
  5. using System.Web;  
  6. using System.Web.Mvc;  
  7. namespace ajaxMVC.Controllers {  
  8.     public class HomeController: Controller {  
  9.         public ActionResult Index() {  
  10.             return View();  
  11.         }  
  12.         public ActionResult About() {  
  13.             ViewBag.Message = "Your application description page.";  
  14.             return View();  
  15.         }  
  16.         public string ShowTime() {  
  17.             Thread.Sleep(2000);  
  18.             return DateTime.Now.ToLongTimeString();  
  19.         }  
  20.         public ActionResult Contact() {  
  21.             ViewBag.Message = "Your contact page.";  
  22.             return View();  
  23.         }  
  24.     }  
  25. }   

Step 6

Now, in _Layout.cshtml, add the line given below.

  1. @Scripts.Render("~/bundles/jqueryval")   

Step 7

Comments in the code itself explains each and everything about the functionality in it.

The code snippet is given below for Index.cshtml. 

  1. @ {  
  2.     ViewBag.Title = "Home Page";  
  3. }  
  4. @ * Below are script references included * @ < script src = "~/Scripts/jquery-1.10.2.js" > < /script> < script src = "~/Scripts/jquery.unobtrusive-ajax.min.js" > < /script> < h1 > Sandip Patil Code Snippets For Ajax.BeginForm Demonstration < /h1>  
  5. @ * In below div tag put Image which is going to display during Ajax Call Initially it is not displaying on page * @ < div id = "Loading"  
  6. style = "display:none" > @ * Here Image is used from Images folder * @ < img src = "~/Images/spinner.gif" / > < /div>  
  7. @ * In below div tag with id = "result"  
  8. where actual result is going to be displaying after successful completion of Ajax Call * @ < div id = "result" > < /div>  
  9. @ * In below Ajax.BeginForm Note LoadingElementId == "Loading"  
  10. for loading Spinner Image loader * @  
  11. @ * In below Ajax.BeginForm Note UpdateTargetId == "result"  
  12. where actual result is going to be displaying after successful completion of Ajax Call * @  
  13. @using(Ajax.BeginForm("ShowTime""Home", FormMethod.Post, new AjaxOptions {  
  14.     OnSuccess = "Clear", LoadingElementId = "Loading", UpdateTargetId = "result", InsertionMode = InsertionMode.Replace  
  15. })) {  
  16.     @ * Below text box with id = "txtBoxInitial"  
  17.     contains initial text = SandipPatilApp * @ < input type = "text"  
  18.     id = "txtBoxInitial"  
  19.     value = "SandipPatilApp" / > < input type = "submit"  
  20.     id = "myBtn"  
  21.     value = "ShowTime" / >  
  22. }  
  23. @ * Now Note OnSuccess = "Clear" in above Ajax.BeginForm this Clear  
  24. function is getting called once Ajax call is over successfully * @ < script type = "text/javascript" > function Clear() {  
  25.     @ * This will clear Textbox with id = "txtBoxInitial"  
  26.     once Ajax call is over successfully * @  
  27.     document.getElementById("txtBoxInitial").value = "";  
  28. } < /script>   

Step 8

Now make sure that in Web.config, the lines given below are included.

  1. <appSettings>  
  2.     <add key="webpages:Version" value="3.0.0.0" />  
  3.     <add key="webpages:Enabled" value="false" />  
  4.     <add key="ClientValidationEnabled" value="true" />  
  5.     <add key="UnobtrusiveJavaScriptEnabled" value="true" />   
  6. </appSettings>   

Step 9

Now, run the Application and you will find it, as shown below in Fig.2


Now, click on ShowTime button.

Once you click it, you will find it as shown below in Fig.3


Now, after a successful AJAX call, it will clear the textbox given above and show the result in UpdateTargetId as current DateTime.Now.ToLongTimeString();

Fig.4 given below demonstrates it.

Ebook Download
View all
Learn
View all