MVC Framework Ajax Support - MVC Framework

How the Ajax supports MVC framework?

Ajax is a well-known shorthand for Asynchronous JavaScript and XML. MVC Framework consists built-in support for unobtrusive Ajax and users can use helper methods for defining Ajax features without adding a code throughout all the views. This feature in MVC is based on the jQuery features.

For enabling unobtrusive AJAX support in MVC application, open Web.Config file and set the UnobtrusiveJavaScriptEnabled property inside appSettings section, using below code. If the key is already present in your application, ignore this step.


After this, open the common layout file _Layout.cshtml file located under Views/Shared folder. Add references to the jQuery libraries here using below code

ajax config

Create an Unobtrusive Ajax Application

In the example shown below, create a form which displays a list of users in the system and place a dropdown which has 3 options: Admin, Normal, and Guest. After selecting one of these values, it displays a list of users belonging to this category using unobtrusive AJAX setup.

Step 1 − Create a Model file Model.cs and copy below code.

Step 2 − Create a Controller file named UserController.cs and create two action methods inside that using below code.

Step 3 − Now create a partial View named GetUserData with below code. This view will be used for rendering a list of users based on the selected role from the dropdown.

Step 4 − Now create a View GetUser with below code. This view will asynchronously get the data from the previously created controller's GetUserData Action.

Step 5 − lastly, change the Route.config entries to launch the User Controller.

Step 6 − Run the application which looks like below screenshot.

ajax application

If Admin is selected from the dropdown, it will go and fetch all the users with Admin type. This is happening via AJAX and does not reload the entire page.

ajax application

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd Protection Status

MVC Framework Topics