In this article, you will learn
- What is @Ajax.ActionLink
- How does this work?
- How to add MicrosoftMvcAjax.js & MicrosoftAjax.js in MVC Project.
- Add a loading image while loading data.
- How to update a div content using @Ajax.ActionLink
Ajax.ActionLink is much like the Html.ActionLink counterpart. It also creates the hyperlink <a href="">Click here</a> but when the user clicks it and has a JavaScript enabled browser, Ajax.ActionLink sends the asynchronous request instead of navigating to the new URL. With the Ajax.ActionLink, we specify what controller's action method is to be invoked and also specify what to do with the response coming back from the action method.
Ajax.ActionLink creates a hyperlink in the browser
- <a class="btn btn-success" data-ajax="true" data-ajax-begin="onAjaxBegin" data-ajax-complete="onAjaxComplete" data-ajax-loading="#divLoading" data-ajax-method="GET" data-ajax-mode="replace" data-ajax-update="#UpdateContentDiv" href="">List Data</a>
Let's create an Ajax.ActionLink helper that will send an asynchronous request to the action method and will update the DOM with the result.

Let's create an MVC Project. If you don't know how to create a simple MVC Project, refer to my previous blogs.
Once you have successfully created a MVC Project, we need to make sure that we have added reference to jquery.unobtrusive-ajax.js and
Once you have successfully created a MVC Project, we need to make sure that we have added reference to jquery.unobtrusive-ajax.js and
jquery.validate.unobtrusive.js in your project.
First, let's add jquery.unobtrusive-ajax.js and jquery.validate.unobtrusive.js in your project. The step is very simple. You can use Nuget Package Manager to install these files or you can do it manually. We use Nuget Package Manger to install these in our project.
Step 1
Right click on Project.
Step 2
Select Manage NuGet Packages.
First, let's add jquery.unobtrusive-ajax.js and jquery.validate.unobtrusive.js in your project. The step is very simple. You can use Nuget Package Manager to install these files or you can do it manually. We use Nuget Package Manger to install these in our project.
Step 1
Right click on Project.
Step 2
Select Manage NuGet Packages.

Step 3
Search for Microsoft.Jquery.Unobtrusive.Ajax and Microsoft.Jquery.Unobtrusive.Validation and Install in your project.
Search for Microsoft.Jquery.Unobtrusive.Ajax and Microsoft.Jquery.Unobtrusive.Validation and Install in your project.

Step 4
After Installation, navigate to Scripts folder of your project. You will be able to notice 4 files are added automatically as in figure
After Installation, navigate to Scripts folder of your project. You will be able to notice 4 files are added automatically as in figure

Now, let's create a method to load data.
Let's assume that you have already added a table called Schools and Teachers in your database. You can use either Code First method or DBFirst Method as per your need. If you want to implement CodeFirst Apprach please refer to my previous blog on Code Forst Approach in MVC 5.
I prefer using Code-First Approach here. So my two modals look like,
Let's assume that you have already added a table called Schools and Teachers in your database. You can use either Code First method or DBFirst Method as per your need. If you want to implement CodeFirst Apprach please refer to my previous blog on Code Forst Approach in MVC 5.
I prefer using Code-First Approach here. So my two modals look like,
School Class

Code Snippet
- public class School
- {
- [Key]
- public int SchoolId { get; set; }
- [Required(ErrorMessage = "Name required")]
- [Column(TypeName = "NVARCHAR")]
- [StringLength(200)]
- [DisplayName("Name")]
- public string Name { get; set; }
- [Required(ErrorMessage = "Address required")]
- [Column(TypeName = "NVARCHAR")]
- [StringLength(200)]
- public string Address { get; set; }
- [Required(ErrorMessage = "Telephone required")]
- [DisplayName("Telephone")]
- [Column(TypeName = "NVARCHAR")]
- [StringLength(200)]
- [Phone]
- public string Phone { get; set; }
- [Required(ErrorMessage = "Email required")]
- //[RegularExpression(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", ErrorMessage = "Wrong email format")]
- [EmailAddress]
- public string Email { get; set; }
- }
Teacher Class
Code Snippet
- //Class: Teacher
- public class Teacher
- {
- [Key]
- public int TeacherId { get; set; }
- [Required(ErrorMessage = "Name required")]
- [Column(TypeName = "NVARCHAR")]
- [StringLength(200)]
- [DisplayName("Name")]
- public string Name { get; set; }
- [Required(ErrorMessage = "Address required")]
- [Column(TypeName = "NVARCHAR")]
- [StringLength(200)]
- public string Address { get; set; }
- [Required(ErrorMessage = "Telephone required")]
- [DisplayName("Telephone")]
- [Column(TypeName = "NVARCHAR")]
- [StringLength(200)]
- [Phone]
- public string Phone { get; set; }
- [Required(ErrorMessage = "Email required")]
- //[RegularExpression(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", ErrorMessage = "Wrong email format")]
- [EmailAddress]
- public string Email { get; set; }
- }
Now, let's Add Controller for Easy CRUD operations.
For now, I used Scaffolding option of MVC to generate method and Views for both Schools and Teachers table. The Controller with Methods for both Schools and Teachers looks like,
For now, I used Scaffolding option of MVC to generate method and Views for both Schools and Teachers table. The Controller with Methods for both Schools and Teachers looks like,

Let's add some data to Database using Create method for both tables.
Creating PartialViews to list Data
Let's add two methods called ListTeachers() and ListSchools() both returning PartialViews() in Home Controller as,

Code Snippet









Dennis KabuguaPosted Jul 19, 2019, 6:13 AM
How do I pass a asp-route-data parameter to a page handler using ajax unobtrusive
RockPosted Jun 7, 2018, 4:06 AM
Hi Thanks Nishan. How to achieve the same in MVC Core 2.0
zekeriya kocaPosted Apr 30, 2018, 4:50 AM
Thanks for sharing.
Vignesh ManiPosted Apr 18, 2017, 4:36 PM
Nice one thank you for sharing.