ExtJS4 & ASP.NET MVC3 Dictionary

See my previous example on getting ExtJS4 working with ASP.NET MVC3

Back when I used to work with ASP.NET AJAX Extensions 1.0 (5 year old tech), I used to do the following all the time:

JavaScript

Ext.Ajax.request({
	url: ...
	, jsonData: { data: form.getForm().getValues() }
	...
}); // eo Ajax

ASP.NET AJAX Extensions 1.0

[WebMethod(EnableSession = true)]
public static void MyWebMethod( Dictionary<string,object> data )
{
	...
}

…ASP.NET AJAX Extensions 1.0 handled the conversion of data from JSON to C# Dictionary, which is a pleasure to work with. To my great disappointment, I discovered that ASP.NET MVC3, the latest and greatest, won’t do that for you! I had to find a solution…

After much Googling and finding out that I’m not the only one in this boat, it came down to having to write a custom extension. I saw many people try, even some examples on GitHub, but nothing that ultimately worked. I got it to the point where I would read the byte input of the Request.InputStream, which for some reason was all 0s.

I couldn’t handle spending any more time on this, so I went for the “hack” below. Please let me know if you have a working solution; I would be glad to post it and provide credit where it’s due.

JavaScript

Ext.Ajax.request({
	url: ...
	, jsonData: { 
		data: Ext.JSON.encode(form.getForm().getValues()) 
	}
	...
}); // eo Ajax

ASP.NET MVC3

[HttpPost]
public void MyWebMethod( string data )
{
	// Parse out form values
	Dictionary<string,object> dictionary = (new System.Web.Script.Serialization.JavaScriptSerializer())
		.Deserialize<Dictionary<string,object>>(data);
 
	...
}

As you see all I’m doing is encoding my JavaScript object into a JSON string, then manually parsing it out on server side. It’s a step back from the 5 year old technology I’m used to, but it works.

Again, if you have a working solution, please comment.

VN:F [1.9.22_1171]
Rating: 9.1/10 (7 votes cast)
ExtJS4 & ASP.NET MVC3 Dictionary, 9.1 out of 10 based on 7 ratings

Leave a Reply

Your email address will not be published. Required fields are marked *

* Copy This Password *

* Type Or Paste Password Here *