Implementing AJAX in ASP.Net MVC

AJAX can be implement by “Ajax library” or by “JQUERY”

AJAX helper Library

Below is a sample of how to implement AJAX by using the “AJAX helper library”. In the below code you can see we have a simple form which is created by using the Ajax.BeginForm syntax. This form calls a controller action called getEmployee. So now the submit action click will be an asynchronous AJAX call.

var AjaxOpt = new AjaxOptions{OnSuccess=”OnSuccess”};
<%} %> </div>

In case you want to make AJAX calls on hyperlink clicks, you can use the Ajax.ActionLink function as shown in the below code.

<% Ajax.ActionLink(“Update”,”Emp”,new AjaxOptions {UpdateTargetId = “EmpDiv” })) %>

In the above code div with the ID as EmpDiv will be submit on the action-link click event.

Ajax using JQUERY

The second way of making an AJAX call in MVC is by using jQuery. In the below code you can see we are making an AJAX POST call to a URL/MyAjax/getEmployee. This is done by using $.post. All this logic is put into a function called GetData and you can make a call to the GetDatafunction on a button or a hyperlink click event as you want.

 

[cn-social-icon]