能够将JQuery Sortable(新订单)保存到ASP.Net MVC控制器吗?

时间:2016-09-23 12:51:44

标签: jquery asp.net-mvc-4 jquery-ui jquery-ui-sortable

我已经实现了JQuery sortable,它工作正常。问题是我无法将新订单中的列表传递给控制器​​,因此我可以将其保存。

如果原始列表如下所示 1 - 饼干 2 - chocalate 3 - 饼干 4 - 糖果

用户拖动和调整

1 - 巧克力 2 - 饼干 3 - 糖果 4 - cookies

如何将新订单传递给我的控制器,以便重新订购模型中的数据。

fatal: unable to access 'https://git.heroku.com/myserver.git/': SSL certificate problem: Invalid certificate chain

1 个答案:

答案 0 :(得分:3)

试试这个

Js代码

$('#MenuItem tbody').sortable({
        axis: 'y',
        update: function (event, ui) {
            var order = 1;
            var model = [];

            $("#MenuItem tbody tr").each(function () {
                //building a new object and pushing in modal array 
                //Here I am setting OrderNo property which is i am using in my db and building my object
                var objModel = { Id: 1, OrderNo: order }; //This is for example to build your object and push in a modal array.
                model.push(objModel);
                order++;
            });

            if (model.length > 1)
            {               
                $.ajax({
                    type: "POST",                            
                    contentType: "application/json; charset=utf-8",
                    url: '@Url.Action("UpdateStatusOrder", "Status")', //This is my url put your url here and pass model as data it is an array of my items
                    data: JSON.stringify({ model : model }),
                    success: function (data) {
                        //do something
                    },
                    error: function (e) {
                        //do something
                    }
                });
            }
        }
    });     

控制器代码

[HttpPost]
public ActionResult UpdateStatusOrder(List<StatusModel> model)
{
     //Update code to update Orderno     
    foreach (var item in model)
    {
        var status = DBContext.Status.Where(x => x.Id == item.Id).FirstOrDefault();
        if (status != null)
        {
            status.OrderNo = item.OrderNo;
        }      
        DBContext.SubmitChanges();
    }
}

注意:这不是您问题的准确答案,但这是同样的情况,并且对我有用,您可以关注它。希望它对你有所帮助。