Using Ajax in Asp.Net MVC
Ajax
So starting of from the beginning Ajax is used for Asynchronous Javascript and XML. We can use it for many purposes. Few basic uses of Ajax are:-
- Update page without reloading the page providing better performance.
- Request data from a server - after the page has loaded which can be used in loading Partial Views.
- Send data to a server without reload - in the background making it easier to performance Save, Delete operations smoothly.
Ajax in Asp.Net MVC
Ajax can be used anywhere where we can use jquery. But we will be watching few examples of different ways of using Ajax in Asp.Net MVC. We will be posting data on MVC Controller without refreshing the page. So let's start from beginning:-
Step-1
So , first of all we will be creating a new view and adding few field on it . Now we will be trying to send the values of these fields on controller without reload and especially using Ajax.
So We will add a new view and few fields on it.
@{ ViewBag.Title = "Home Page"; } <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> <div class="jumbotron"> <h2>Ajax Call Getting started</h2> </div> <div class="form-group"> <label for="email">Employee Id:</label> <input type="text" class="form-control" id="txtId" placeholder="Enter Employee Id" name="email"> </div> <div class="form-group"> <label for="pwd">Employee Name:</label> <input type="text" class="form-control" id="txtName" placeholder="Enter Employee Name" name="pwd"> </div> <div class="form-group"> <label for="pwd">Employee Salary:</label> <input type="text" class="form-control" id="txtSalary" placeholder="Enter Employee Salary" name="pwd"> </div> <input type="button" id="btnGet" class="btn btn-success" value="Ajax Call Type 1" /> <input type="button" id="btnGet2" class="btn btn-success" value="Ajax Call Type 2" /> <input type="button" id="btnGet3" class="btn btn-success" value="Ajax Call Type 3" /> <input type="button" id="btnGet4" class="btn btn-success" value="Ajax Call Type 4" /> <input type="button" id="btnGet5" class="btn btn-success" value="Ajax Call Type 5" /> <script type="text/javascript"> $(function () { $("#btnGet").click(function () { var empIds = $("#txtId").val(); var empNames = $("#txtName").val(); var empSalarys = $("#txtSalary").val(); $.ajax({ type: "POST", url: "/Home/AjaxMethod", data: '{empId: "' + empIds + '" , empName: "' + empNames + '" , empSalary: "' + empSalarys + '" }', contentType: "application/json; charset=utf-8", dataType: "json", success: function (response) { alert("Hello: " + response.EmpName + " Your Employee Id Is: " + response.EmpId + "And Your Salary Is: " + response.EmpSalary); }, failure: function (response) { alert(response.responseText); }, error: function (response) { alert(response.responseText); } }); }); }); // second $(function () { $("#btnGet2").click(function () { debugger; var empIds = $("#txtId").val(); var empNames = $("#txtName").val(); var empSalarys = $("#txtSalary").val(); $.ajax({ url: "/Home/AjaxMethod", dataType: "json", type: "POST", cache: false, data: { empId: empIds, empName: empNames, empSalary: empSalarys }, success: function (response) { alert("Hello: " + response.EmpName + " Your Employee Id Is: " + response.EmpId + "And Your Salary Is: " + response.EmpSalary); }, failure: function (response) { alert(response.responseText); }, error: function (response) { alert(response.responseText); } }) }) }); //third $(function () { $("#btnGet3").click(function () { var intrestedInAll = { EmpId: $("#txtId").val(), EmpName: $("#txtName").val(), EmpSalary: $("#txtSalary").val(), }; debugger; $.ajax({ url: '/Home/AjaxMethodWithObject', type: 'POST', data: { "queryFilter": JSON.stringify(intrestedInAll) }, cache: false, success: function (response) { alert("Hello: " + response.EmpName + " Your Employee Id Is: " + response.EmpId + "And Your Salary Is: " + response.EmpSalary); }, failure: function (response) { alert(response.responseText); }, error: function (response) { alert(response.responseText); } }); }); }); //fourth $(function () { $("#btnGet4").click(function () { var personModel = { EmpId: $("#txtId").val(), EmpName: $("#txtName").val(), EmpSalary: $("#txtSalary").val(), }; personModel = JSON.stringify(personModel); debugger; $.ajax({ type: "POST", url: "/Home/AjaxMethodWithModel", data: personModel, dataType: "json", contentType: 'application/json; charset=utf-8', success: function (response) { alert("Hello: " + response.EmpName + " Your Employee Id Is: " + response.EmpId + "And Your Salary Is: " + response.EmpSalary); }, failure: function (response) { alert(response.responseText); }, error: function (response) { alert(response.responseText); } }); }); }); //fifth function GetAjaxDataPromise(url, postData) { debugger; var promise = $.post(url, postData, function (promise, status) { }); return promise; }; $(function () { $("#btnGet5").click(function () { debugger; var promises = GetAjaxDataPromise('@Url.Action("AjaxMethod", "Home")', { EmpId: $("#txtId").val(), EmpName: $("#txtName").val(), EmpSalary: $("#txtSalary").val() }); promises.done(function (response) { debugger; alert("Hello: " + response.EmpName + " Your Employee Id Is: " + response.EmpId + "And Your Salary Is: " + response.EmpSalary); }); }); }); </script>
@{ ViewBag.Title = "Home Page"; } <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> <div class="jumbotron"> <h2>Ajax Call Getting started</h2> </div> <div class="form-group"> <label for="email">Employee Id:</label> <input type="text" class="form-control" id="txtId" placeholder="Enter Employee Id" name="email"> </div> <div class="form-group"> <label for="pwd">Employee Name:</label> <input type="text" class="form-control" id="txtName" placeholder="Enter Employee Name" name="pwd"> </div> <div class="form-group"> <label for="pwd">Employee Salary:</label> <input type="text" class="form-control" id="txtSalary" placeholder="Enter Employee Salary" name="pwd"> </div>
<input type="button" id="btnGet" class="btn btn-success" value="Ajax Call Type 1"> <input type="button" id="btnGet2" class="btn btn-success" value="Ajax Call Type 2"> <input type="button" id="btnGet3" class="btn btn-success" value="Ajax Call Type 3"> <input type="button" id="btnGet4" class="btn btn-success" value="Ajax Call Type 4"> <input type="button" id="btnGet5" class="btn btn-success" value="Ajax Call Type 5">
$(function () { $("#btnGet").click(function () { var empIds = $("#txtId").val(); var empNames = $("#txtName").val(); var empSalarys = $("#txtSalary").val(); $.ajax({ type: "POST", url: "/Home/AjaxMethod", data: '{empId: "' + empIds + '" , empName: "' + empNames + '" , empSalary: "' + empSalarys + '" }', contentType: "application/json; charset=utf-8", dataType: "json", success: function (response) { alert("Hello: " + response.EmpName + " Your Employee Id Is: " + response.EmpId + "And Your Salary Is: " + response.EmpSalary); }, failure: function (response) { alert(response.responseText); }, error: function (response) { alert(response.responseText); } }); }); });
[HttpPost] public JsonResult AjaxMethod(string empId, string empName, string empSalary) { PersonModel person = new PersonModel { EmpId = empId, EmpName = empName, EmpSalary = empSalary }; return Json(person); }
// second $(function () { $("#btnGet2").click(function () { debugger; var empIds = $("#txtId").val(); var empNames = $("#txtName").val(); var empSalarys = $("#txtSalary").val(); $.ajax({ url: "/Home/AjaxMethod", dataType: "json", type: "POST", cache: false, data: { empId: empIds, empName: empNames, empSalary: empSalarys }, success: function (response) { alert("Hello: " + response.EmpName + " Your Employee Id Is: " + response.EmpId + "And Your Salary Is: " + response.EmpSalary); }, failure: function (response) { alert(response.responseText); }, error: function (response) { alert(response.responseText); } }) }) });
[HttpPost] public JsonResult AjaxMethod(string empId, string empName, string empSalary) { PersonModel person = new PersonModel { EmpId = empId, EmpName = empName, EmpSalary = empSalary }; return Json(person); }
//third $(function () { $("#btnGet3").click(function () { var intrestedInAll = { EmpId: $("#txtId").val(), EmpName: $("#txtName").val(), EmpSalary: $("#txtSalary").val(), }; debugger; $.ajax({ url: '/Home/AjaxMethodWithObject', type: 'POST', data: { "queryFilter": JSON.stringify(intrestedInAll) }, cache: false, success: function (response) { alert("Hello: " + response.EmpName + " Your Employee Id Is: " + response.EmpId + "And Your Salary Is: " + response.EmpSalary); }, failure: function (response) { alert(response.responseText); }, error: function (response) { alert(response.responseText); } }); }); });
[HttpPost] public JsonResult AjaxMethodWithObject(string queryFilter) { PersonModel personModel = JsonConvert.DeserializeObject<PersonModel>(queryFilter); return Json(personModel); }
//fourth $(function () { $("#btnGet4").click(function () { var personModel = { EmpId: $("#txtId").val(), EmpName: $("#txtName").val(), EmpSalary: $("#txtSalary").val(), }; personModel = JSON.stringify(personModel); debugger; $.ajax({ type: "POST", url: "/Home/AjaxMethodWithModel", data: personModel, dataType: "json", contentType: 'application/json; charset=utf-8', success: function (response) { alert("Hello: " + response.EmpName + " Your Employee Id Is: " + response.EmpId + "And Your Salary Is: " + response.EmpSalary); }, failure: function (response) { alert(response.responseText); }, error: function (response) { alert(response.responseText); } }); }); });
[HttpPost] public JsonResult AjaxMethodWithModel(PersonModel personModel) { PersonModel person = new PersonModel { EmpId = personModel.EmpId, EmpName = personModel.EmpName, EmpSalary = personModel.EmpSalary }; return Json(person); }
//fifth function GetAjaxDataPromise(url, postData) { debugger; var promise = $.post(url, postData, function (promise, status) { }); return promise; }; $(function () { $("#btnGet5").click(function () { debugger; var promises = GetAjaxDataPromise('@Url.Action("AjaxMethod", "Home")', { EmpId: $("#txtId").val(), EmpName: $("#txtName").val(), EmpSalary: $("#txtSalary").val() }); promises.done(function (response) { debugger; alert("Hello: " + response.EmpName + " Your Employee Id Is: " + response.EmpId + "And Your Salary Is: " + response.EmpSalary); }); }); });
[HttpPost] public JsonResult AjaxMethod(string empId, string empName, string empSalary) { PersonModel person = new PersonModel { EmpId = empId, EmpName = empName, EmpSalary = empSalary }; return Json(person); }
[HttpPost] public JsonResult AjaxMethodWithModel(PersonModel personModel) { PersonModel person = new PersonModel { EmpId = personModel.EmpId, EmpName = personModel.EmpName, EmpSalary = personModel.EmpSalary }; return Json(person); }
The complete code for controller is
public class HomeController : Controller { public ActionResult Index() { return View(); } [HttpPost] public JsonResult AjaxMethod(string empId, string empName, string empSalary) { PersonModel person = new PersonModel { EmpId = empId, EmpName = empName, EmpSalary = empSalary }; return Json(person); } [HttpPost] public JsonResult AjaxMethodWithObject(string queryFilter) { PersonModel personModel = JsonConvert.DeserializeObject<PersonModel>(queryFilter); return Json(personModel); } [HttpPost] public JsonResult AjaxMethodWithModel(PersonModel personModel) { PersonModel person = new PersonModel { EmpId = personModel.EmpId, EmpName = personModel.EmpName, EmpSalary = personModel.EmpSalary }; return Json(person); } }
Response-
So When jquery ajax request completed you can check the response from success method. You can check in the examples above. So this is how we can use Ajax in Asp.Net MVC.