ASP.NET MVC - How to Use Ajax with Parameters

Ajax methodology in web development can or cannot have input query parameters for processing/loading web parts on the web page. However, complex logic and improved performance measures demand Ajax call to pass multiple input query parameters whether in a typical multiple parameter format or JSON input format.
 
Today, I shall be demonstrating the integration of Ajax calls by passing multiple input query parameters with the ASP.NET MVC5 platform.
 
 
Prerequisites
 
The following are some prerequisites before you proceed any further in this tutorial,
  1. Knowledge of Jquery.
  2. Knowledge of HTML.
  3. Knowledge of JavaScript.
  4. Knowledge of Bootstrap.
  5. Knowledge of ASP.NET MVC5.
  6. Knowledge of C# Programming.
The example code is being developed in Microsoft Visual Studio 2019 Professional.
 
Let's begin now.
 
Step 1
 
Create a new MVC web project and name it "MVCAjaxWithParam".
 
Step 2
 
Create a "Controllerss\HomeController.cs" file with default Index method and GetData(...) method with two input query parameters for Ajax call with following lines of code i.e.
  1. ...  
  2.         public ActionResult GetData(int customerID, string fname = "")  
  3.         {  
  4.             // Initialization.  
  5.             JsonResult result = new JsonResult();  
  6.             DataTable data = DataTable();  
  7. ...  
  8.             // Load Data.  
  9. ...  
  10.             // Filter data with input query parameters.  
  11. ...                  
  12.             // Prepare Ajax JSON Data Result.  
  13.             result = this.Json(JsonConvert.SerializeObject(data), JsonRequestBehavior.AllowGet);  
  14. ...  
  15.             // Return info.  
  16.             return result;  
  17.         }  
  18. ... 
In the above code, I have created a simple “GetData(…)” method with ActionResult returning type for client-side ajax calling to return result data in JSON format. I have first loaded my data in DataTable structure, then I have applied the input query filter with OR condition and finally, I have prepared my JSON response.
 
Step 3
 
Now, create the subsequent view "Views\Home\Index.cshtml"of "Controllerss\HomeController.cs" index method and add table HTML tag as shown below i.e.
  1. ...  
  2.     <table class="table table-responsive table-striped table-bordered table-hover"  
  3.            id="TableId"  
  4.            cellspacing="0"  
  5.            align="center"  
  6.            width="100%">  
  7.     </table>  
  8. ... 
In the above code, I have created a simple responsive Bootstrap style HTML table tag structure. I will populate the data in this table using Ajax call.
 
Step 4
 
Now, create the JavaScript file "Scripts\script-custom-ajax.js" with the following lines of code i.e.
  1. ...  
  2.   
  3. $(document).ready(function ()  
  4. {  
  5.     // Initialization  
  6.     var customerID = 23;  
  7.     var firstname = "John";  
  8.   
  9.     $.ajax(  
  10.         {  
  11.             type: 'POST',  
  12.             dataType: 'JSON',  
  13.             url: '/Home/GetData',  
  14.             data: { customerID: customerID, fname: firstname },  
  15.             success:  
  16.                 function (response)  
  17.                 {  
  18.                     // Generate HTML table.  
  19.                     convertJsonToHtmlTable(JSON.parse(response), $("#TableId"));  
  20.                 },  
  21.             error:  
  22.                 function (response)  
  23.                 {  
  24.                     alert("Error: " + response);  
  25.                 }  
  26.         });  
  27. ...   
  28.   
  29. });  
  30.   
  31. ... 
In the above code, I have made an Ajax call to my server-side at the load of the page to get input query base filter data, since I am now passing multiple query parameters to the Ajax call. After receiving the processed JSON data from the server-side you can utilize any logic of your own to convert the JSON data to HTML table.
 
Step 5
 
Now, execute the provided solution and you will be able to see the following in action i.e.
 
 
When I only pass single "CustomerID" input query parameter then I will get the following result as shown below i.e.:
 
 

Conclusion

 
In this article, you will learn integration of Ajax calls by passing multiple input query parameters with ASP.NET MVC5 platform. You will also learn to create a server-side method, which will be called by client-side Ajax call using Jquery. You will learn to make simple client-side Ajax calls at the load of the page and finally, you will learn to pass multiple input query parameters to the Ajax call.