3

質問:

私はjQueryソート可能で作業しています。このチュートリアルを使用しました:
http://www.xmech.net/programming/jquery-ui-sortable-tutorial/

これで、次のような文字列が得られます。

ID[]=2&ID[]=3&ID[]=4&ID[]=1

さらに悪いことに、ID の IDa_1、IDb_2、IDc_3、IDd_4 を呼び出すと、

IDb[]=2&IDc[]=3&IDd[]=4&IDa[]=1

私はこのフォーマットが恐ろしく役に立たないと思っています...ページだけが欲しいです:["IDb_2"、"IDc_3"、"IDd_4"、"IDa_1"]と配列インデックスの要素の順序を持​​っています。

これを修正するために、次のことを行いました。

var xxx =  { pages: $(this).sortable('toArray') };
alert(JSON.stringify(xxx));

これは私のホームコントローラーです:

public string SaveSortable(string pages)
{
    string strPages = Request.Params["pages"];

    Console.WriteLine(pages);
    return pages;
}


    public ActionResult Sortable()
    {
        return View();
    } // End Action Sortable

問題は、「ページ」と strPages の両方が常に null
であることです...ただし、正しいコントローラーに入ります...
何が間違っていますか?

これは私の .cshtml です:

@{
    Layout = null;
}

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title></title>

    <style type="text/css" media="all">
        html {margin: 0px; padding: 0px; height: 100%;}
        body {margin: 0px; padding: 0px; height: 100%;}
        input{margin: 0px; padding: 0px; }
    </style>



    <style type="text/css" media="all">

         .menu li 
         {
            list-style: none;
            padding: 10px;
            margin-bottom: 5px;
            border: 1px solid #000;
            background-color: #C0C0C0;
            width: 150px;
            display: inline;
        }
    </style>

    <script type="text/javascript" src="@Url.Content("~/Scripts/jQuery/jquery-1.7.2.min.js")"></script>  
    <script type="text/javascript" src="@Url.Content("~/Scripts/jQuery/jquery-ui/1_8_21/js/jquery-ui-1.8.21.custom.min.js")"></script>  


    <script type="text/javascript" language="javascript">

        Date.prototype.getTicksUTC = function () {
            return Date.parse(this.toUTCString()) + this.getUTCMilliseconds();
        } // End Function getTicksUTC


        var tickURL = '@Url.Content("~/Scripts/jQuery/SlickGrid/images/tick.png")';


        $(document).ready(function () {
            //$('#menu-pages').sortable();
            $("#menu-pages").sortable({
                update: function (event, ui) {
                    alert("posting");

                    //var ElementsOrder = $(this).sortable('toArray').toString();
                    var ElementsOrder = $(this).sortable('toArray');//.toString();
                    //alert(JSON.stringify(ElementsOrder));

                    var xxx =  { pages: $(this).sortable('toArray') };
                    //alert(JSON.stringify(xxx));
                    //document.writeln("<h1>"+ JSON.stringify(xxx) + "</h1>");


                    // $.post("@Url.Action("SaveSortable", "Home")?no_cache=" + new Date().getTicksUTC(), { pages: $('#menu-pages').sortable('serialize') });
                    // $.post("@Url.Action("SaveSortable", "Home")?no_cache=" + new Date().getTicksUTC(), { pages: $('#menu-pages').sortable('serialize', { key: 'id' }) });

                    $.post("@Url.Action("SaveSortable", "Home")?no_cache=" + new Date().getTicksUTC(), { pages: $(this).sortable('toArray') });

                }
            });

        });

    </script>

</head>
<body>
    <ul class="menu" id="menu-pages">
        <li id="ID_1">Home</li>
        <li id="ID_2">Blog</li>
        <li id="ID_3">About</li>
        <li id="ID_4">Contact</li>
    </ul>
</body>
</html>
4

1 に答える 1

6

JSON リクエストを使用するだけです。

$('#menu-pages').sortable({
    update: function (event, ui) {
        $.ajax({
            url: '@Url.Action("SaveSortable", "Home")',
            type: 'POST',
            cache: false,
            contentType: 'application/json',
            data: JSON.stringify({ pages: $(this).sortable('toArray') }),
            success: function(result) {
                // ...
            }
        });
    }
});

その後:

[HttpPost]
public ActionResult SaveSortable(string[] pages)
{
    // pages will contain what you need => a list of ids
    ...
}

記録として、JSON リクエストの POST ペイロードは次のようになります。

{"pages":["ID_2","ID_3","ID_1","ID_4"]}
于 2012-08-03T07:12:35.877 に答える