Ajax calls are simple asynchronous requests to the servers to load the resources for your application. In this example I will consider that your page (which will handle the ajax request) has the partial view loading code present, and the view only generates the HTML content for the menu.
Ajax requests are available natively in the browsers, you only need to configure your ASP.NET web application to accept the ajax requests and return the response which is configured to be sent as a snippet (
do not send entire HTML DOM in ajax requests, there is no purpose of ajax requests if you have to download entire HTML markup). Same thing for sending the resources to the server. To submit a form using ajax (
because your purpose to send ajax requests is to ignore the form postback, stopping page reloading) you can add an
event.preventDefault()
to the ajax handler so that the form doesn't submit and you send your own data to the server.
event.preventDefault();
$.ajax({
url: 'ajax-controller/form-handler',
data: $('#myform').serialize(),
success: function (data) {
}
});
I have written a post for beginners in topics such as,
Ajax in ASP.NET MVC. It covers how you should perform these actions in ASP.NET MVC, perhaps a separate controller is best choice! Also you may return a string in case of ajax controllers, so that HTML DOM is not returned by the layouts and so.
A tip for ajax developers in ASP.NET MVC framework[
^]