Calling A C# Function With jQuery AJAX In ASP.NET MVC

jQuery AJAX Example 

jQuery AJAX method is a very powerful function for doing asynchronous operations from the Web pages. It helps to update parts of a Web page without reloading the whole page. This increases the Website's speed and reduces the load. It can be used with any Web technology like HTML, ASP.NET, ASP.NET MVC, PHP, Ruby or any other.

With jQuery AJAX method, you can create high performance features in our Website. Let me show you how to use this method in  ASP.NET MVC page.


To understand the working of jQuery AJAX, I will create a login feature in ASP.NET MVC page. The page will have 2 input controls to enter the username and the password. There will also be a button to submit the two values for checking from the database.

When the username and password are correct, the secured information of the user is shown, else the “wrong username and password” is shown.

The main attraction here is that I will use jQuery AJAX method to check the username and the password. There will be no page reloading during checking.

To start learning jQuery AJAX method, I would suggest you check – jQuery AJAX. Also, look for the syntax and the key-value pairs that can be passed to this method.

The Controller Code

Start with creating a “Controller” in your ASP.NET MVC Application. Now, add a C# function “GetSecuredData” into the controller.

  1. [HttpPost]  
  2. public string GetSecuredData(string userName, string password)  
  3. {  
  4.     string securedInfo = "";  
  5.     if ((userName == "admin") && (password == "admin"))  
  6.         securedInfo = "Hello admin, your secured information is .......";  
  7.     else  
  8.         securedInfo = "Wrong username or password, please retry.";  
  9.     return securedInfo;  
  10. }  


The C# function given above will be called by jQuery AJAX method. As you can see, this function has 2 parameters, “username” and “password”. In these 2 parameters, it receives the username and the password values. It then checks them and shows the secured information, if they are the correct ones.

You can also change the code given above to include the database operation, where the username and the password are checked against the ones stored in the database.

The View code

Now, create a view for the Controller. This view will contain the two input controls for the username and password. There will be a button, which when clicked will call jQuery AJAX function. 

  1. <h3>Enter the Username and Password:</h3>  
  2. (enter "admin" for both username and password)  
  3. <input type="text" id="usernameInput" placeholder="Username" />  
  4. <input type="text" id="passwordInput" placeholder="Password" />  
  5. <button id="submit">Submit</button>  
  6. <div id="dataDiv"></div>  
  7. <script src=""></script>  
  8. <script>  
  9.     $(document).ready(function () {  
  10.         $("#submit").click(function (e) {  
  11.             if ($("#usernameInput").val() == "")  
  12.                 alert("Username cannot be empty");  
  13.             else if ($("#passwordInput").val() == "")  
  14.                 alert("Password cannot be empty");  
  15.             else {  
  16.                 $.ajax({  
  17.                     type: "POST",  
  18.                     url: "/Home/GetSecuredData",  
  19.                     contentType: "application/json; charset=utf-8",  
  20.                     data: '{"userName":"' + $("#usernameInput").val() + '","password":"' + $("#passwordInput").val() + '"}',  
  21.                     dataType: "html",  
  22.                     success: function (result, status, xhr) {  
  23.                         $("#dataDiv").html(result);  
  24.                     },  
  25.                     error: function (xhr, status, error) {  
  26.                         $("#dataDiv").html("Result: " + status + " " + error + " " + xhr.status + " " + xhr.statusText)  
  27.                     }  
  28.                 });  
  29.             }  
  30.             return false;  
  31.         });  
  32.     });  
  33. </script>  


The button click event will call jQuery AJAX event. We pass the “controller name/function name” to the URL field. The jQuery AJAX event will call the C# function which gets the username and password values in its parameters.

Finally in the success function we will show the returned value from the C# function.

That’s all for the code, isn’t it really simple?


I hope, you like this article on jQuery AJAX usage in ASP.NET MVC. Please share your feedback.

Similar Articles