1

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.

2
  • Did you check myResult in the debugger? You may be missing an & Commented Nov 25, 2014 at 15:46
  • sorry, that was just a typo in the question. I have updated the question Commented Nov 25, 2014 at 15:48

3 Answers 3

1

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
Sign up to request clarification or add additional context in comments.

4 Comments

Good question. There is a separate unrelated reason why I can't use ajax here (because the controller action is returning a File(). I added that point to my question as I can imagine that others will ask the same thing
Is MyItem something stored server-side that you could look up? Do you need all the properties on MyItem?
no, MyItem is completely generated clientside from the user updating the jquery ui sortable
I haven't tried yet but in theory i think your solution should work forme
1

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

Comments

0

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

Comments

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.