title: Update value by using DataTables row-reorder in ASP.Net MVC 5
body: <p>I need to update the <code>OrderIndex</code> value inside database by row-reorder function in datatables (<a href="http://www.datatables.net" rel="nofollow">www.datatables.net</a>).</p>
  
  <p>I have already searched in google and I found this <a href="http://www.codeproject.com/Articles/331986/Table-Row-Drag-and-Drop-in-ASP-NET-MVC-jQuery-Data" rel="nofollow">article</a>. I followed all of the steps but it didn't help.</p>
  
  <p>Please let me know if you need more information. Thank you</p>
  
  <h2>ViewModel</h2>
  
  <pre><code>public class GenderDDUpdateOrderIndexViewModel
      {
        public int Id { get; set; }
        public int OrderIndex { get; set; }
      }
  </code></pre>
  
  <h2>Controller</h2>
  
  <pre><code>public async Task&lt;ActionResult&gt; 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 =&gt; (toPosition &lt;= c.OrderIndex &amp;&amp; c.OrderIndex &lt;= fromPosition))
                         .ToList();
  
                foreach (var gender in GenderList)
                    {
                     gender.OrderIndex++;
                    }
                  }
                  else
                  {
              var GenderList = ctx.GenderDD
                         .Where(c =&gt; (fromPosition &lt;= c.OrderIndex &amp;&amp; c.OrderIndex &lt;= toPosition))
                         .ToList();
                 foreach (var gender in GenderList)
                    {
                     gender.OrderIndex--;
                    }
                  }
           ctx.GenderDD.First(c =&gt; c.Id == Id).OrderIndex = toPosition;
           await ctx.SaveChangesAsync();
       }
           return View();
       }
  </code></pre>
  
  <h2>DataTables Initialisation</h2>
  
  <pre><code> &lt;script&gt;
          $(document).ready(function() {
  
              var table = $('#GenderIndex').DataTable({
                  "paging": true,
                  "pagingType": "full_numbers",
                  rowReorder: {
                   snapX: 10  
                  }
  
              });         
  
         });
  &lt;/script&gt;
  </code></pre>