ASP.NET MVC, JSON & non JavaScript clients

I need to ensure that an application I am developing is accessable and also works with JavaScript turned off. I just need a pointer to assist with the following.

I had 3 'chained' select boxes and I wanted JavaScript enabled clients to have a nice Ajax experience. I can easily write the required functionality to populate the chained boxes on the change event of the preceeding select using jQuery and JSON with a WCF service. However what about the non JavaScript client?

Would I wrap a submit next to the select and place these inside their own form to post back with a certain action or different querstring parameter? Can the same controller give me a partial JSON response as well as feeding the full HTML response. Can anyone point me to a good demo that utilises both JSON and normal HTTP posts to produce the same result in ASP.NET MVC. All ASP.NET MVC demo/examples I see forget about the non JavaScript enabled client.

Update

But isn't that true for Ajax calls using the Microsoft Ajax client library if I read it corectly - which I am wanting to avoid and use only jQuery - apologies should have mentioned that.

Also I would prefer not to put that noise everywhere in the controllers (reminds me of ispostback from webforms...shudder).

It's a shame there is no attribute that I can use on a controller like with [AcceptVerbs(HttpVerbs.Post)] but for content types e.g [AcceptType(httpTypes.Json)].

There must be a better way than using that if statement everywhere.....

Answers


This article has an elegant solution for your problem: http://devlicio.us/blogs/sergio_pereira/archive/2008/12/05/reusing-views-in-asp-net-mvc-for-ajax-updates.aspx


You can check the IsMvcAjaxRequest property and use it inside your controller and then return a partial view (user control) or JSON result if true, or the full View if it's false.

Something like this:

public ActionResult List()
{
   if (!Request.IsMvcAjaxRequest())
   {
       // Non AJAX requests see the entire ViewPage.
       return View();
   }
   else
   {
       // AJAX requests just get a trimmed down UserControl.
       return Json(...);
   }
 } 

More info here: MVC AJAX Sites That Gracefully Degrade


(ref. your previous answer): You could pass a value in your Request.Form to signal that this is a browser with or without javascript enabled, and then create a controller factory that will instantiate the appropriated controller according to what you get in Request.Form; a default controller for regular requests (browsers with javascript enabled) and a "Fallback controller" that only returns full views instead. I am not sure if having twice the number of controllers is better than those if-else statements around a single controller, but I guess it's a question of personal preference.

I bet there are simpler solutions, though..I wish this question had more exposure.


Need Your Help

BCP/ Bulk Insert Fails (tab delimited file)

sql sql-server bulkinsert bcp tab-delimited

I have been trying to import data (tab delimited) into SQL server. The source data is exported from IBM Cognos. Data can be downloaded from: sample data

Why do I need to '@import compass' in all my sass files for precompilation?

ruby-on-rails asset-pipeline compass-sass

I'm using Rails and Compass. Everything works great until I deploy to production at which point I hit: