Introduction
This is another quick article for the ASP.NET developers, who're using Ajax (or will use the Ajax in their applications for async calls for the data to the server), and might be getting confused in the View-part of the MVC pattern. This article attempts to provide a tip for them with a simple method for creating Ajax functionality in their applications and how to get the confusion about View-part clear. The background to this article is a question that arose, of how to use the Ajax to get the data in the ASP.NET MVC framework. Although that was a simple task, I know new developers get into trouble in making the logic. That is why I am writing this article, to explain the core-concept of the behavior.
I will also be providing sample code that you can download and test in your application. This application would be a simple “Hello world, using Ajax in ASP.NET MVC” application. You can then make this application as complex as you require.
Getting hands dirty in some ASP.NET code
The first stage would be to write the back-end code for the application that is the ASP.NET MVC application. Wait, the first thing I want to elaborate on here is that Ajax requests don't usually require you to send an entire HTML markup, to be rendered. Usually, Ajax requests are used to download just a few details about the user, such as their name, their company name, or a little message such as a success message that the process user has started. That is why, sending an entire HTML markup would take somuch time and network size, by sending the entire XML tree as HTML markup.
That is why, I will not-create any View in this project. But I will work with just a single controller. That controller will be used to do all the actions and after the controller has been made, I will use the Route mechanism to allow custom URLs in the application that do not map to our actual HTML files on the application set up. Don't worry, as the application is built up, the concept of this paragraph will grow and you will understand better how I did it and what you will do to create your own Ajax running ASP.NET MVC application.
Note: If you're unaware of the ASP.NET MVC framework itself, I urge you to please go and read my other article that focuses on the ASP.NET MVC framework itself. In this article, I will not explain any concept about the framework itself, instead I will just simply mix things up and explain the Ajax and ASP.NET MVC part. So, it would be better if you move on to that article and first understand the underlying concepts about Model, View and Controller (that makes up the MVC part of the ASP.NET MVC framework).
First of all, let us create a controller. To create a controller, you can get some help from Visual Studio. Click on the Controllers folder and Add Controller to it. Name it to whatsoever you want to. I named it AjaxController to specify that it would control all of the Ajax requests over HTTP protocol. Once that has been done, you can create your own action methods that would respond to your own personal functions. I didn't bother creating any complex task, instead I just simply created a function, named “CustomAction”. It will be used as the URL of our Ajax handling web page.
Inside that page, you will rovide the following code, to handle it and provide the result to it. Like a simple C# application, the functions can have any return data type and thus, in an ASP.NET MVC application we can, instead of a View, return a string (or any other data type object) that would be sent down to the client as the response to his Ajax request. It would be much simpler and much shorter in structure.

Nitesh KejriwalPosted Dec 16, 2015, 8:31 AM
Nice read!
Sachin KaliaPosted Sep 23, 2015, 4:12 PM
Excellent Share and informative
Bruno PétersonPosted Apr 25, 2015, 11:42 AM
Really, it's very simple. Thanks!
Tom MohanPosted Mar 23, 2015, 4:45 AM
Nice
Manish Kumar ChoudharyPosted Mar 22, 2015, 1:18 AM
Nice one.