ASP.NET MVC 2 ExtJs and Ajax post array of objects to controller?
I'm developing an ASP.NET MVC2 web application.
I want to send an array of JSON objects from my view code using AJAX to the controller. I have seen many examples of hot to do this using jquery. However I would like to know how to do this using an Ajax request without using jquery? I have read that updating to MVC3 may help, if this is the best solution can you point me in the right direction on how to update from MVC2 to MVC3?Below is some sample code:
VIEW
var modRecords = store.getModifiedRecords();
Ext.Ajax.request({
url: AppRootPath +'EmployeeDetails/SetSAASUser',
params: {
users: modRecords
}
});
C开发者_如何学PythonONTROLLER
public JsonResult SetUser(IEnumerable<User> users)
{
GetData data = delegate
{
return Repo.SetUser(users);
};
JsonResultBase jsonResult = GetJsonResult(data);
JsonResult json = PortalJsonResult(jsonResult, JsonRequestBehavior.AllowGet);
return json;
}
To MVC3 or not to MVC3
No particular need to convert to MVC3 though because you can consume JSON in MVC2 as well. There are two actually many ways of doing it:
using
JsonValueProviderFactory
which Phil Haack described in his blog post that would give you exactly equivalent functionality as if you've used MVC3.Pre-convert your client data so ExtJS will correctly send it to the server. This is somehow similar to what I've done with a jQuery plugin. A very similar thing could be done with ExtJS as well. These two steps are necessary to accomplish it:
First you'd need to analyse how your JSON object is converted on the wire (use Fiddler)
Write code that transforms your JSON into a form that will be correctly sent to the server. What form would that be? You can read about that in my previously mentioned blog post.
I don't know if you're aware of this but there's also something called Ext.Direct for ASP.NET MVC that may help you in this scenario. As it says it support simple, complex and array parameters which covers it actually.
The only advantage of using MVC3 is that JsonValueProviderFactory
is enabled for you by default without any additional code.
I've used ExtJS few years ago when it was in version 2. No Ext.Direct
and MVC back then yet. But we were very successful in pairing it to an Asp.net WebForms application with async calls to WCF using the same business+data layers as Asp.net WebForms application used.
精彩评论