With asp.net-mvc, can I include stringified json into the query string or a url?

1.5k views Asked by At

I have the following code that i am using to run a controller action on my asp.net-mvc site:

asp.net-mvc Controller Action:

public ActionResult MyAction(PostParams myParams)
{
   //go do stuff
}

where PostParams is defined as:

public class PostParams
{
     public int Age {get;set;}
     public string Name {get;set;}
}

Javascript Code:

$("#myButton").live('click', function (e) {
    window.location.href = "/MyController/MyAction?Age=10&Name=Joe";
});

NOTE: Before someone responds with "You should be using Ajax", I am unable to use ajax here because my controller action is returning a file as per this question here.

I now need to pass in some additional data which is the result of a jquery UI sortable list like this which comes in as json. so that looks like this:

$("#myButton").live('click', function (e) {

    var sortables = $(".sortableList");

    var arr = [];
    sortables.each(function () {

    var statusParam = new Object();
    statusParam.Ids = $(this).sortable("toArray");
    arr.push(statusParam);
});

    var myResult = JSON.stringify({ result: arr });

    window.location.href = "/MyController/MyAction?Age=10&Name=Joe";
});

My question is, what is the correct way to include the "myResult" into the query string where it will show up on the server side correctly. I have tried this:

    var myResult = JSON.stringify({ result: arr });

    window.location.href = "/MyController/MyAction?" + myResult + "&Age=10&Name=Joe";
});

and included a new field in the PostParams like this:

 public class PostParams
 {
     public List<MyItem> result {get;set;}
     public int Age {get;set;}
     public string Name {get;set;}
 }

 public class MyItem
 {
     public List<int> Ids { get; set; }
 }

but result always seems to be null when I check it on the server side.

3

There are 3 answers

4
Steve Greatrex On BEST ANSWER

I don't know if this is actually not supported but I don't think you should be trying to post stringified data in the URL.

You could very easily end up with a huge URL!

Why not POST the required data using $.post instead of updating the URL? Or if that isn't possible, why not append an ID to the URL instead of an entire object then look that ID up on the server side?

Update

If you have to return a File then you could try this approach:

  1. POST the necessary data to the server
  2. Server stores the information in temporary data against a GUID and returns the GUID
  3. Client receives the GUID and uses it in another URL that can be set against window.location.href
  4. The action for the new URL grabs the data from temporary data and returns the File
0
Marian Ban On

you can try to build your url like this:

window.location.href = "/My/MyAction?result[0].ids=1&result[1].ids=2&result[2].ids=3&result[2].ids=4&Age=10&Name=Joe";

params:

enter image description here

0
Mohd. Shaukat Ali On

Try this..

window.location.href = "/MyController/MyAction?result=" + JSON.stringify(arr) + "&Age=10&Name=Joe";

beacause model binder needs "result" key to bind the data to result property of your veiw model