1

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
            }
}); 

CONTROLLER

    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;
    }
3
  • Ok so if I understand you, you're trying to accomplish the same thing but using ExtJS instead of jQuery? Commented Apr 6, 2011 at 7:57
  • Yes or else just plain javascript no jQuery.. Any thoughts on this? Commented Apr 6, 2011 at 8:37
  • Not sure what your problem was here but did you try users: Ext.encode(modRecords)? That converts modRecords into valid JSON that can be sent to the server. Commented Feb 29, 2012 at 20:08

1 Answer 1

2

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:

  1. using JsonValueProviderFactory which Phil Haack described in his blog post that would give you exactly equivalent functionality as if you've used MVC3.

  2. 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.

  3. 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.

Sign up to request clarification or add additional context in comments.

5 Comments

Have you successfully tried using Ext.Direct MVC to send complex Json data? I ask because I'm getting stuck with it.
@DavidS: not really but ExtJS support is great (I used to use it few years ago). People there will surely help you. If you've bought license they have to answer in certain amount of time. So... Ask them.
We are still in the trial phase but thanks for getting back to me. I think I've figured out how to get things going :). Also, I doubt they'd be supporting Ext.Direct MVC :).
@DavidS: As I can see on their forum there are quite some questions related to Ext.Direct and MVC. Forum participation is free you know.
Yes I am part of the forums. Effectively what I've found out is this. For complex objects, one needs to use batch operations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.