开发者

How do I process a serialized string in ASP.NET MVC?

I probably haven't worded the title of this 开发者_Python百科question particularly well in the slightest.

I'm a bit of a newbie when it comes to a lot of JSON bits and pieces and currently, I have a nested sortable plugin that produces the following serialized string:

list[1]=root&list[2]=root&list[3]=2&list[4]=2&list[5]=2&list[6]=2&list[7]=root&list[8]=root&list[9]=root&list[10]=root&list[11]=10&list[12]=10&list[13]=10&list[14]=10&list[15]=root&list[16]=root

Which is all well and good, but I've not a clue how I process it in my controller. I've had a Google and couldn't find anything specific, but I think my search terms were as poorly worded as my question title.

Can someone point me in the right direction please?

Thanks for any help in advance.


This is how I do it in MVC 2. edit: I also use the json2.js script mentioned in the Haacked article

part of the HTML:

 <ul class="sortList">
    <% foreach(var item in Model){ %>

    <li  id="item_<%= item.ID %>">

the jQuery:

 $(".sortList").sortable(
        {
            connectWith: ".sortList",
            containment: "document",
            cursor: "move",
            opacity: 0.8,
            placeholder: "itemRowPlaceholder",
            update: function(event, ui) {
                $.post("/Admin/UpdateSortOrder/", { sortlist: $(this).sortable("serialize") });
            }
        });

the value being posted:

"item_0d2243bf-e01d-4049-964c[]=d69b92009072&item_bab23d45-442b-4178-817c[]bbdea32ff226&item_e987ed37-cf30-4413-8687[]=9dc8d111482a"

The action

[HttpPost]
        public ActionResult UpdateSortOrder(string sortlist)
        {
            string[] separator = new string[2] { "item_", "&" };
            string[] tempArray = sortlist.Split(separator, StringSplitOptions.RemoveEmptyEntries);

            if (tempArray.Length > 0)
            {
                for (int i = 0; i < tempArray.Length; i++)
                {
                    tempArray[i] = tempArray[i].Replace("[]=", "-");
                    Guid _id = new Guid(tempArray[i]);
                    var temp = _session.Single<Photo>(x => x.ID == _id);
                    temp.Sortorder = i + 1;
                    _session.Update(temp);
                    _session.CommitChanges();
                }

            } return Content("volgorde aangepast");
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜