Post The Data To ASP.NET MVC Controller Using JQuery Ajax

This blog will demonstrate, how to post the data to ASP.Net MVC controller(s) using JQuery Ajax. For that, I have created one controller "JQueryAjaxCallController" with the post action method "AjaxPostCall" and a class "Employee" as below.

Controller 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. namespace JQueryAjaxCallINMVC.Controllers {  
  7.     public class JQueryAjaxCallController: Controller {  
  8.         //  
  9.         // GET: /JQueryAjaxCall/  
  10.         public ActionResult Index() {  
  11.                 return View();  
  12.             }  
  13.             [HttpPost]  
  14.         public JsonResult AjaxPostCall(Employee employeeData) {  
  15.             Employee employee = new Employee {  
  16.                 Name = employeeData.Name,  
  17.                     Designation = employeeData.Designation,  
  18.                     Location = employeeData.Location  
  19.             };  
  20.             return Json(employee, JsonRequestBehavior.AllowGet);  
  21.         }  
  22.     }  
  23.     public class Employee {  
  24.         public string Name {  
  25.             get;  
  26.             set;  
  27.         }  
  28.         public string Designation {  
  29.             get;  
  30.             set;  
  31.         }  
  32.         public string Location {  
  33.             get;  
  34.             set;  
  35.         }  
  36.     }  
  37. }  
View 

Next step is to add a View for the Controller and while adding it you will need to select the Employee class created earlier. 

 

Once you click the Add button, it will create strongly typed view with Employee model. Now we need to create Razor/Html controls to get the user input. Here, I used the HTML controls to get user input and we made a jQuery Ajax Post to call MVC controller in the below code.
  1. @model JQueryAjaxCallINMVC.Controllers.Employee  
  2. @ {  
  3.     Layout = null;  
  4. } < html > < head > < meta name = "viewport"  
  5. content = "width=device-width" / > < title > JQUERY AJAX CALL < /title> < script type = "text/javascript"  
  6. src = "https://code.jquery.com/jquery-1.12.4.js" > < /script> < script type = "text/javascript" > $(function() {  
  7.     $("#btnPost").click(function() {  
  8.         var employee = new Object();  
  9.         employee.Name = $('#txtName').val();  
  10.         employee.Address = $('#txtDesignation').val();  
  11.         employee.Location = $('#txtLocation').val();  
  12.         if (employee != null) {  
  13.             $.ajax({  
  14.                 type: "POST",  
  15.                 url: "/JQueryAjaxCall/AjaxPostCall",  
  16.                 data: JSON.stringify(employee),  
  17.                 contentType: "application/json; charset=utf-8",  
  18.                 dataType: "json",  
  19.                 success: function(response) {  
  20.                     if (response != null) {  
  21.                         alert("Name : " + response.Name + ", Designation : " + response.Designation + ", Location :" + response.Location);  
  22.                     } else {  
  23.                         alert("Something went wrong");  
  24.                     }  
  25.                 },  
  26.                 failure: function(response) {  
  27.                     alert(response.responseText);  
  28.                 },  
  29.                 error: function(response) {  
  30.                     alert(response.responseText);  
  31.                 }  
  32.             });  
  33.         }  
  34.     });  
  35. }); < /script> < /head> < body > < div style = "margin-left:20px" > JQuery Ajax POST call wtih ASP.NET MVC controller < /div> < br / > < div title = "PostPortion" > < input type = "text"  
  36. id = "txtName"  
  37. placeholder = "Name" / > < input type = "text"  
  38. id = "txtDesignation"  
  39. placeholder = "Designation" / > < input type = "text"  
  40. id = "txtLocation"  
  41. placeholder = "Location" / > < input type = "button"  
  42. id = "btnPost"  
  43. value = "Post Data" / > < /div> < /body> < /html>  
Detailed Description


The JSON.stringify() method converts a JavaScript value to a JSON string. It's optional when we are just passing parameter(s) with Values(s) 

RouteConfig.Cs

You need to set your startup controller and view.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using System.Web.Routing;  
  7. namespace JQueryAjaxCallINMVC {  
  8.     public class RouteConfig {  
  9.         public static void RegisterRoutes(RouteCollection routes) {  
  10.             routes.IgnoreRoute("{resource}.axd/{*pathInfo}");  
  11.             routes.MapRoute(name: "Default", url: "{controller}/{action}/{id}", defaults: new {  
  12.                 controller = "JQueryAjaxCall", action = "Index", id = UrlParameter.Optional  
  13.             });  
  14.         }  
  15.     }  
  16. }  
Now run your application 

 

Refer to the attached demo project and I hope it's helpful.
Ebook Download
View all
Learn
View all