views:

399

answers:

1

I am currently developing a new website utilizing ASP.NET MVC2. Much of what I want to do with the website (from a front-end standpoint) involves AJAX-y-type tasks.

Two examples:

  1. I want to display forms, and switch between them via a drop-down menu (or through links) without having to go back and hit the server every single time.
  2. I want the user to be able to select points on a Chart Control and have portions of the page refresh with the appropriate data depending on what he/she selects (partial page refresh).

In both these cases, I am able to accomplish what I wish to accomplish using the traditional web forms event handlers, etc. Unfortunately, the use of a ScriptManager violates the spirit of MVC. It seems as if MVC prevents the use of many of the controls that are available to ASP.NET.

So, my question is: how do I use AJAX and make AJAX calls without utilizing ScriptManager and the web forms method of utilizing event handlers?

+2  A: 

It isn't that ScriptManager "violates the spirit" of MVC, it's just that the MVC framework is built differently.

Web controls rely on ASP.Net webforms constructs such as ViewState and Postbacks. These don't exist in ASP.Net MVC, so any controls relying on them will fail to function properly. However, controls will still render their HTML and run their event handlers, as each .aspx page still goes through the page lifecycle when it is compiled by the default view engine. (If you're using a custom view engine to render your html, they won't work at all!)

Anyway, most people rely on Microsoft's ajax scripts (MicrosoftAjax.js, MicrosoftMvcAjax.js) or use a third party library such as jQuery to do their ajax stuff. If you're just getting started, I would suggest heading over to the main ASP.Net MVC site and checking out some of their tutorials. The NerdDinner ebook is a great start to MVC, and there is a chapter on using Microsoft's Ajax to do dynamic updates.

womp