通过在ASP.Net MVC 5中使用DataTables行重新排序来更新值
我需要通过datatables( www.datatables.net )中的row-reorder函数更新数据库中的OrderIndex
值。
我已经在谷歌搜索过,我发现了这篇文章 。 我遵循了所有步骤,但没有帮助。
如果您需要更多信息,请与我们联系。 谢谢
视图模型
public class GenderDDUpdateOrderIndexViewModel { public int Id { get; set; } public int OrderIndex { get; set; } }
调节器
public async Task 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 <= c.OrderIndex && c.OrderIndex c.Id == Id).OrderIndex = toPosition; await ctx.SaveChangesAsync(); } return View(); }
DataTables初始化
$(document).ready(function() { var table = $('#GenderIndex').DataTable({ "paging": true, "pagingType": "full_numbers", rowReorder: { snapX: 10 } }); });
上面提到的插件不起作用,因为它非常陈旧。 您可以轻松使用DataTables RowReorder扩展。 您不需要编写这样的长动作方法,因为最新版本的DataTables不支持其中的许多参数。
调节器
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的