本文介绍了通过使用数据表行重新排序在ASP.Net MVC 5更新值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要更新数据表数据库里面的 OrderIndex 价值由行重新排序功能(的)。

我已经在谷歌搜索,我发现这个的。我跟所有的步骤,但它并没有帮助。

请让我知道如果你需要更多的信息。谢谢

视图模型

 公共类GenderDDUpdateOrderIndexViewModel
    {
      公众诠释标识{搞定;组; }
      公众诠释OrderIndex {搞定;组; }
    }

控制器

 公共异步任务<&的ActionResult GT; UpdateRowIndex(GenderDDUpdateOrderIndexViewModel模型,诠释?标识,诠释?fromPosition,诠释toPosition,串方向)
 {
   使用(VAR CTX =新ApplicationIdentityDbContext())
      {
        如果(方向==回)
          {
            VAR GenderList = ctx.GenderDD
                       。凡(C =>(toPosition< = c.OrderIndex和放大器;&安培; c.OrderIndex< = fromPosition))
                       .ToList();              的foreach(在GenderList VAR性别)
                  {
                   gender.OrderIndex ++;
                  }
                }
                其他
                {
            VAR GenderList = ctx.GenderDD
                       。凡(C =>(fromPosition< = c.OrderIndex和放大器;&安培; c.OrderIndex< = toPosition))
                       .ToList();
               的foreach(在GenderList VAR性别)
                  {
                   gender.OrderIndex--;
                  }
                }
         ctx.GenderDD.First(C => c.Id ==编号).OrderIndex = toPosition;
         等待ctx.SaveChangesAsync();
     }
         返回查看();
     }

的DataTable的初始化

 <脚本>
        $(文件)。就绪(函数(){            无功表= $('#GenderIndex')。数据表({
                分页:真实,
                pagingType:full_numbers,
                rowReorder:{
                 snapX:10
                }            });       });
< / SCRIPT>


解决方案

上面提到的插件将无法正常工作,因为这是非常老。您可以轻松地使用扩展。
你不需要因为许多这些参数都没有数据表的最新版本,支持写这么长的操作方法。

控制器

 公共无效UpdateRow(GenderDDUpdateOrderIndexViewModel模型,诠释标识,诠释fromPosition,诠释toPosition)
        {
            使用(VAR CTX =新ApplicationIdentityDbContext())
            {
               变种GenderList = ctx.GenderDD.ToList();
               ctx.GenderDD.First(C => c.OrderIndex ==编号).OrderIndex = toPosition;
               ctx.SaveChanges();
            }
        }

的jQuery

 <脚本类型=文/ JavaScript的>
        无功表= $('#GenderIndex')。数据表({
            rowReorder:真实,
              分页:真实,
              pagingType:full_numbers,
              fixedHeader:真实,
              选择:假的,
              序:[0,ASC]],
              信息:真实,
              加工:假的,
              有求必应:真实,
              搜索:真实,
              lengthMenu:[[10,25,50,-1],[10,25,50,全部],
              语言: {
                  lengthMenu:_MENU_,
                  zeroRecords:没有找到记录,
                  infoEmpty:没有找到记录,
                  infoFiltered:(从_MAX_总记录数过滤)
              },
              columnDefs:[
                  {订购:假的,目标:8}
              ]
        });        $('#GenderIndex')。在('行reorder.dt',函数(的dragEvent,数据节点){
            对于(VAR I = 0,IEN = data.length; I< IEN;我++){
                VAR rowData = table.row(数据[I] .node)。数据();
                $阿贾克斯({
                    键入:GET,
                    缓存:假的,
                    的contentType:应用/ JSON的;字符集= UTF-8,
                    网址:'/ GenderDD / UpdateRow',
                    数据:{编号:rowData [0],fromPosition:数据[I] .oldData,toPosition:数据[I] .newData}
                    数据类型:JSON
                });
                }
        });
   < / SCRIPT>

I need to update the OrderIndex value inside database by row-reorder function in datatables (www.datatables.net).

I have already searched in google and I found this article. I followed all of the steps but it didn't help.

Please let me know if you need more information. Thank you

ViewModel

public class GenderDDUpdateOrderIndexViewModel
    {
      public int Id { get; set; }
      public int OrderIndex { get; set; }
    }

Controller

public async Task<ActionResult> UpdateRowIndex(GenderDDUpdateOrderIndexViewModel model, int? Id, int? fromPosition, int toPosition, string direction)
 {
   using (var ctx = new ApplicationIdentityDbContext())
      {
        if (direction == "back")
          {
            var GenderList = ctx.GenderDD
                       .Where(c => (toPosition <= c.OrderIndex && c.OrderIndex <= fromPosition))
                       .ToList();

              foreach (var gender in GenderList)
                  {
                   gender.OrderIndex++;
                  }
                }
                else
                {
            var GenderList = ctx.GenderDD
                       .Where(c => (fromPosition <= c.OrderIndex && c.OrderIndex <= toPosition))
                       .ToList();
               foreach (var gender in GenderList)
                  {
                   gender.OrderIndex--;
                  }
                }
         ctx.GenderDD.First(c => c.Id == Id).OrderIndex = toPosition;
         await ctx.SaveChangesAsync();
     }
         return View();
     }

DataTables Initialisation

 <script>
        $(document).ready(function() {

            var table = $('#GenderIndex').DataTable({
                "paging": true,
                "pagingType": "full_numbers",
                rowReorder: {
                 snapX: 10
                }

            });

       });
</script>
解决方案

Above mentioned plug-in won't work since it's extremely old. You can easily use DataTables RowReorder extension.You don't need to write such a long action method since many of those parameters are not supported by the latest version of DataTables.

Controller

public void UpdateRow(GenderDDUpdateOrderIndexViewModel model, int Id, int fromPosition, int toPosition)
        {
            using (var ctx = new ApplicationIdentityDbContext())
            {
               var GenderList = ctx.GenderDD.ToList();
               ctx.GenderDD.First(c => c.OrderIndex == Id).OrderIndex = toPosition;
               ctx.SaveChanges();
            }
        }

jQuery

<script type="text/javascript">
        var table = $('#GenderIndex').DataTable({
            rowReorder: true,
              "paging": true,
              "pagingType": "full_numbers",
              fixedHeader: true,
              select: false,
              "order": [[0, "asc"]],
              "info": true,
              "processing": false,
              "responsive": true,
              "searching": true,
              "lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
              "language": {
                  "lengthMenu": "_MENU_",
                  "zeroRecords": "No records found",
                  "infoEmpty": "No records found",
                  "infoFiltered": "(filtered from _MAX_ total records)"
              },
              "columnDefs": [
                  { "orderable": false, "targets": 8 }
              ]
        });

        $('#GenderIndex').on('row-reorder.dt', function (dragEvent, data, nodes) {
            for (var i = 0, ien = data.length ; i < ien ; i++) {
                var rowData = table.row(data[i].node).data();
                $.ajax({
                    type: "GET",
                    cache: false,
                    contentType: "application/json; charset=utf-8",
                    url: '/GenderDD/UpdateRow',
                    data: { Id: rowData[0], fromPosition: data[i].oldData, toPosition: data[i].newData },
                    dataType: "json"
                });
                }
        });
   </script>

这篇关于通过使用数据表行重新排序在ASP.Net MVC 5更新值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 21:03