jQuery的可排序.toArray的ASP.NET MVC的ActionResult [英] jQuery Sortable .toArray with ASP.NET MVC ActionResult

查看:170
本文介绍了jQuery的可排序.toArray的ASP.NET MVC的ActionResult的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第三次尝试固定在今晚 - 努力现在比以前不同的方法。

Third try at fixing this tonight - trying a different approach than before now.

如果提供的jQuery排序列表。

Given a jQuery Sortable List..

<ul id="sortable1" class="connectedSortable">
    <li class="ui-state-default" id="item1">Item 1</li>
    <li class="ui-state-default" id="item2">Item 2</li>
    <li class="ui-state-default">Item 3</li>
    <li class="ui-state-default ">Item 4</li>
    <li class="ui-state-default">Item 5</li>
</ul>

<ul id="sortable2" class="connectedSortable">
</ul>

和ASP.NET MVC的ActionResult。

And ASP.NET MVC ActionResult..

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Insert( string[] items )
    {
        return null;
    }

由JavaScript激活...

Activated by JavaScript...

        $("#sortable1, #sortable2").sortable({
            connectWith: '.connectedSortable',
            dropOnEmpty: true,
            receive: function () {
                var items = $(this).sortable('toArray');
                alert(items);
                $.ajax({
                    url: '/Manage/Events/Insert',
                    type: 'post',
                    data: { 'items': items }
                });
            }
        }).disableSelection();

在'警报'不显示正确的项目。它显示了项目1,ITEM2等,但我的ASP.NET MVC的ActionResult将一无所获。该方法不火,但'项目'参数进来空。任何想法?

The 'alert' DOES show the right items. It shows 'item1, item2' etc. But my ASP.NET MVC ActionResult gets nothing. The method DOES fire, but the 'items' parameter comes in null. Any ideas?

推荐答案

貌似序列化问题在jQuery的1.4.2解决(见<一href=\"http://stackoverflow.com/questions/2515773/ajax-post-of-javascript-string-array-to-jsonresult-as-liststring-always-return\">related问题)。

Looks like serialization issue was addressed in jQuery 1.4.2 (See related question).

原来的答复:结果
我八九不离十想通了答案,但你很可能不喜欢它=)。

Original answer:
I sorta figured out the answer, but you will most likely not like it =).

纵观表单提交的例子上菲尔哈克的博客,只要你输入元素都有他们就会被放入一个列表相同的名称。我试过字符串数组转换成对象的这样一个数组:

Looking at the form post example on Phil Haack's blog, as long as your input elements all have the same name they'll be put into a list. I tried mapping the array of strings to an array of objects like so:

var items = $.map( 
    $(this).sortable('toArray'),
    function(item, index){
        return {"item": item};
    }
);

$.ajax({
    url: '/Home/Insert',
    type: 'post',
    data: { items: items }
});

,当我做了我提交了正确长度的非空数组,但每个单元为空。问题是,(根据萤火虫)邮政PARAMS是这样的:

and when I did the submit I got a non null array of the correct length, but each cell was null. The problem was that (according to firebug) the POST params looked like this:

items[0][item]  item1
items[1][item]  item2

这是接近我想要的,但也不能令人信服。我真正想要的提交是一些看起来是这样的:

which is close to what I want, but not quite there. What I really wanted to submit was something that looks like this:

items   item1
items   item2

因为这本质上是什么菲尔的例子的形式在做什么。我不知道怎么去JSON序列化一样,所以我被骗了,用手创建的字符串,像这样:

because that's essentially what the form in Phil's example is doing. I don't know how to get JSON to serialize like that so I cheated and created the string by hand like so:

$.ajax({
    url: '/Home/Insert',
    type: 'post',
    data: 'items=items1&items=items2'
});

这工作。当我检查了项目数组中的行动的价值,它有两个字符串为item1的和item2的。所以它看起来像你可以得到你的code通过手动序列化数据进行工作(这么说),使用字符串作为你的Ajax调用的参数。不知怎的,这感觉就像是错误的方式去,所以我希望有人纠正我的方式,是/感觉更合适的。

This worked. When I checked the value of the items array in the action, it had two strings in it "item1" and "item2". So it looks like you can get your code to work by serializing the data by hand (so to speak) and using the string as an argument in your ajax call. Somehow this feels like the wrong way to go, so I hope someone corrects me with a way that is/feels more right.

这篇关于jQuery的可排序.toArray的ASP.NET MVC的ActionResult的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆