Visual Studio 2010!

Read now >

View Now
DevSource RSS FEEDS
XML Want an easy way to keep up with breaking tech news? And the Get DevSource headlines delivered to your desktop with RSS.
ADVERTISEMENT
ADVERTISEMENT

 

DevSource.com: Your Source for Visual Studio on Facebook
ADVERTISEMENT
Advice On ASP.Net MVC, With AJAX
By DevSource

Rate This Article: Add This Article To:

In this article from DotNetSlackers, Brian Mains breaks down how best to plan for implementing AJAX into your web applications that use MVC.

To view the full article in its entirety, please visit DotNetSlackers: Complexity in ASP.NET MVC, Part 2: Plan your AJAX Carefully

Introduction
ASP.NET web forms chose the option of communicating with web services as its mode for communication between client-side JavaScript and server-side code. A web service, using the ScriptService attribute, can render a client-side proxy to call these web methods, provided that it doesn't go outside the bounds of the current web domain (a browser restriction).

ASP.NET MVC differs from this methodology a bit; instead of using web services, client-side code can directly invoke an action method. This means all of the code can reside in a controller, but be invoked in various ways, either by client-side JavaScript or a direct URL request.

What this can mean is that controllers may container a lot of mixed code serving different purposes. Action methods may need to render the entire view or only render part of it. Either way, it's important to develop a strategy to handle it.

Understanding the Problem
Let's look at what I'm trying to illustrate. Suppose we have this action method:

Listing 1: Serving the View
public ActionResult DoThis()
{
var repos = RepositoryFactory.Create();
var data = repos.GetAll();
Return View(data);
}

If the browser requests this view, an ASPX page serves the request through the associated view. However, if JQuery requests the action method as in Listing 2, this is an entirely different type of request.

Listing 2: Using JQuery
$.get("/Ctlr/DoThis", funtion(data) { });

We can return the full view, but usually JQuery only needs to replace a portion of it. Typically, setting up a partial view to replace via JQuery is a better approach because we can swap out one set of content for another, essentially. Previously, we talked about how to use the Request.IsAjaxRequest extension method to check for AJAX requests, how to invoke views via JavaScript, and setup complex action methods. Here we are going to look at that level of planning AJAX in MVC.




Discuss Advice On ASP.Net MVC, With AJAX
 
>>> Be the FIRST to comment on this article!
 

 
 
>>> More ASP and .Net Coding Techniques Articles          >>> More By DevSource