Welcome to Dovetail Software Blogs : Sign in | Join | Help
jQuery 1.4 breaks ASP.Net MVC actions with array parameters

We had a Ajax-ified page break on us today and eventually figured out that the cause was related to an upgrade of jQuery and in particular a change in how jQuery 1.4 does serialization of array parameters for Ajax operations. From the jQuery 1.4 Release Notes.

jQuery 1.4 adds support for nested param serialization in jQuery.param, using the approach popularized by PHP, and supported by Ruby on Rails. For instance, {foo: ["bar", "baz"]} will be serialized as “foo[]=bar&foo[]=baz”.

In jQuery 1.3, {foo: ["bar", "baz"]} was serialized as “foo=bar&foo=baz”. However, there was no way to encode a single-element Array using this approach. If you need the old behavior, you can turn it back on by setting the traditional Ajax setting (globally via jQuery.ajaxSettings.traditional or on a case-by-case basis via the traditional flag).

Our page was using getJSON to get data back from an ASP.Net MVC action that looks like this

public JsonResult GetListElements(string listTitle, string[] levelValues)

We found that the second array parameter would always be null. Adding the recommended Ajax setting for the page corrected the issue.

// Enables for all serialization
jQuery.ajaxSettings.traditional = true;

For more details about this issue check out this post: jQuery 1.4 breaks ASP.NET MVC parameter posting.

Posted: Wednesday, February 24, 2010 4:46 PM by kmiller
Filed under: ,

Comments

Joshua Flanagan said:

That's funny. I had "foo[]=bar&foo[]=baz" (or something similar) written on the whiteboard not too long ago, wondering if anyone had seen it before (no one (on our side of the room) had). I hadn't tied it to jQuery 1.4, so thanks!

# February 24, 2010 9:43 PM

kmiller said:

@Josh you know thinking about this more this seems like a ModelBinder issue. The MVC model binder should be aware of this convention.

# February 25, 2010 8:39 AM

uberVU - social comments said:

This post was mentioned on Twitter by elijahmanor: "jQuery 1.4 breaks ASP.Net MVC actions with array parameters" by @kevm #tech #jquery #aspnetmvc http://j.mp/c5bJun

# February 26, 2010 3:50 AM

Raif said:

Hey,  I just got pooched by the same freakin issue.  Small world.  I agree that this is a short coming of the model binder.  But for now I'll just set my flag.

Thanks for calling it out so I know I'm not crazy.  Or not as crazy as I had thought.

R

# March 25, 2010 10:19 AM