Sending Ajax Request when Dropdown change in ASP.NET MVC

Introduction

This is a simple example which send an ajax request to the ActionResult and return a JsonResult.

jQuery script for sending the request

jQuery(document).ready(function(){
$("#YourDropDownId").change(function() {
var ServiceUrl ="/YourController/Action?id=" + 1;
var content = '';
$.support.cors = true;
$.ajax({
type: 'GET',
url: ServiceUrl,
async: true,
cache: false,
crossDomain: true,
contentType: "application/json; charset=utf-8",
dataType: 'json',
error: function (xhr, err) {
},
success: function (result, status) {
$('#yourTextId').val(result.Value);
}
});
});
});

ActionResult

[HttpGet]
public ActionResult Action(int id)
{
var result = new { Value = "my Json result" };
return Json(result, JsonRequestBehavior.AllowGet);
}

Enjoy Coding…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s