ASP.NET MVC - How To Use Ajax without Passing Parameters

In order to improve UI interactivity in web development, it is often recommended to utilize Ajax to load/process small web parts/components independently on the same page.
 
Today, I shall be demonstrating the integration of Ajax call without passing any parameters with the ASP.NET MVC5 platform.
 
ASP.NET MVC - How To Use Ajax without Passing Parameters
 
Prerequisites
 
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 "MVCAjaxNoParam".  
 
Step 2
 
Create a "Controllerss\HomeController.cs" file with default Index method and GetData(...) method for ajax call with the following lines of code:
  1. ...  
  2.         public ActionResult GetData()  
  3.         {  
  4.             // Initialization.  
  5.             JsonResult result = new JsonResult();  
  6. ...  
  7.             // Initialization.  
  8.             DataTable data = DataTable();  
  9.   
  10.             // Loading Data.  
  11. ...  
  12.             // Prepare Json response.  
  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 and simply loaded a sample data in DataTable structure and then 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:
  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:
  1. ...  
  2.   
  3. $(document).ready(function ()  
  4. {  
  5.     $.ajax(  
  6.         {  
  7.             type: 'POST',  
  8.             dataType: 'JSON',  
  9.             url: '/Home/GetData',  
  10.             success:  
  11.                 function (response)  
  12.                 {  
  13.                     // Generate HTML table.  
  14.                     convertJsonToHtmlTable(JSON.parse(response), $("#TableId"));  
  15.                 },  
  16.             error:  
  17.                 function (response)  
  18.                 {  
  19.                     alert("Error: " + response);  
  20.                 }  
  21.         });  
  22. ...  
  23.     function convertJsonToHtmlTable(jsonData, tableSelector)  
  24.     {  
  25. ...  
  26.         // Initialization.  
  27.         var convertJsonToHTML;  
  28.   
  29.         // Process received data.  
  30. ...  
  31.         // Append received data to table  
  32.             $(tableSelector).append(convertJsonToHTML);  
  33.         }  
  34.     }   
  35. });  
  36.   
  37. ... 
In the above code, I have made an Ajax call to my server-side at the loading of the page to get all the data, since, I am not passing any parameter to the Ajax call. I have processed the received data from the server side and converted the json data to HTML table.
 
Step 5
 
Now, execute the provided solution and you will be able to see the following in action:
 
ASP.NET MVC - How To Use Ajax without Passing Parameters
 

Conclusion

 
In this article, you leanred integration of Ajax calls without passing any parameters with ASP.NET MVC5 platform. You also learned to create server-side method which will be called by client-side ajax call using Jquery and how to make simple client-side Ajax calls at the load of the page without passing any parameters to the Ajax call.