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.
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:window.location.href
File