将 jQuery 可排序序列化结果传递到 ASP.NET Razor 页面
这是“我想通了,但很痛苦,所以我发布这个来帮助其他人”的问题之一。
我正在 ASP.NET Razor 上构建一个基于 jQuery 的应用程序。我正在使用 jQuery.ui sortable 来对事物进行排序。
对于我来说,如何将可排序事件的结果传递到 Razor 页面并不明显。有很多 PHP 的示例,但我找不到 Razor 的任何示例。
下面是一个可排序的 jQuery.ui 示例:
$('#Categories').sortable({
update: function () {
var catOrder = $(this).sortable("serialize").toString();
$.ajax({
type: "POST",
url: "OrderCategories",
data: catOrder,
}).done(function (msg) {
alert('done: ' + msg);
});
}
});
它将一个如下所示的字符串传递到 OrderCategories
页面:
{id[]=2&id[]=3&id[]=1&id[]=4&id[]=5}
显然 ASP.NET 足够聪明,可以找出像数组一样的查询字符串。要获取这个数组,您所要做的就是
var order = Request.Params["id[]"];
现在 order
是一个表示列表顺序的整数数组。我花了太长时间才弄清楚这一点。希望这有帮助。
This is one of those "I figured it out but it was painful so I'm posting this to help others" questions.
I'm building a jQuery based app on ASP.NET Razor. I am using jQuery.ui sortable to enable sorting of things.
It was non-obvious to me how to pass the results of a sortable event to my Razor page. There are lots of examples for PHP, but I couldn't find anything for Razor.
Here's an example jQuery.ui for sortable:
$('#Categories').sortable({
update: function () {
var catOrder = $(this).sortable("serialize").toString();
$.ajax({
type: "POST",
url: "OrderCategories",
data: catOrder,
}).done(function (msg) {
alert('done: ' + msg);
});
}
});
This passes a string that looks like this to the OrderCategories
page:
{id[]=2&id[]=3&id[]=1&id[]=4&id[]=5}
Apparently ASP.NET is smart enough to figure out a query string like this is an array. All you have to do to get this array is
var order = Request.Params["id[]"];
Now order
is an array of integers representing the order of the list. Took me way too long to figure this out. Hope this helps.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以上就是答案。希望这对其他人有用。
The above is the answer. Hope this is useful to others.