【发布时间】:2015-01-23 16:34:05
【问题描述】:
我有以下代码用于在我的 asp.net-mvc 站点上运行控制器操作:
asp.net-mvc 控制器操作:
public ActionResult MyAction(PostParams myParams)
{
//go do stuff
}
其中 PostParams 定义为:
public class PostParams
{
public int Age {get;set;}
public string Name {get;set;}
}
Javascript 代码:
$("#myButton").live('click', function (e) {
window.location.href = "/MyController/MyAction?Age=10&Name=Joe";
});
注意:在有人回复“您应该使用 Ajax”之前,我无法在此处使用 ajax,因为我的控制器操作正在返回文件 as per this question here。
我现在需要传入一些额外的数据,这些数据是 jquery UI sortable list like this 的结果,它以 json 格式出现。所以看起来像这样:
$("#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";
});
我的问题是,将“myResult”包含在查询字符串中的正确方法是什么,它将正确显示在服务器端。我试过这个:
var myResult = JSON.stringify({ result: arr });
window.location.href = "/MyController/MyAction?" + myResult + "&Age=10&Name=Joe";
});
并在 PostParams 中包含一个新字段,如下所示:
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; }
}
但是当我在服务器端检查时,result似乎总是为空。
【问题讨论】:
-
您是否在调试器中检查了
myResult?您可能缺少& -
对不起,这只是问题中的一个错字。我已经更新了问题
标签: jquery asp.net-mvc json jquery-ui-sortable stringify