Multi Select And Multi Draggable Rows Within Grid In ASP.NET MVC

We will look into how to make your tabular data in cshtml file into a  Grid and make that grid's rows multi selectable and multi draggable within the grid with the help of jQuery UI and jQuery datatables plug-in.

We will create a solution in Visual Studio 2015 step by step.

  1. Open Visual Studio 2015, click on New Project, select ASP.NET Web Application, name it as “DraggableGrid”.



  2. Now, click OK button. Select template as “MVC”.



  3. Now, click OK button. It will create an empty MVC Application.

  4. Add new class inside Model folder, named “State”.



  5. Add the properties given below inside the class.
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Web;  
    5.   
    6. namespace DraggableGrid.Models  
    7. {  
    8.     public class State  
    9.     {  
    10.         public int StateID { get; set; }  
    11.         public string StateName { get; set; }  
    12.         public string StateCapital { get; set; }  
    13.         public string Population { get; set; }  
    14.     }  
    15. }  
  6. Add new repository class inside model folder named “StateRepository”.
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Web;  
    5.   
    6. namespace DraggableGrid.Models  
    7. {  
    8.     public class StateRepository  
    9.     {  
    10.         public static IList<State> states = null;  
    11.   
    12.         public static IList<State> GetStates()  
    13.         {  
    14.             if(states == null)  
    15.             {  
    16.                 states = new List<State>();  
    17.                 states.Add(new State() { StateID = 1, StateName = "Alabama", StateCapital = "Montgomery", City = "Birmingham" });  
    18.                 states.Add(new State() { StateID = 2, StateName = "Arizona", StateCapital = "Phoenix", City = "Phoenix" });  
    19.                 states.Add(new State() { StateID = 3, StateName = "Alaska", StateCapital = "Juneau", City = "Juneau" });  
    20.                 states.Add(new State() { StateID = 4, StateName = "California", StateCapital = "Sacramento", City = "Los Angeles" });  
    21.                 states.Add(new State() { StateID = 4, StateName = "Colorado", StateCapital = "Denver", City = "Denver" });  
    22.                 states.Add(new State() { StateID = 5, StateName = "Connecticut", StateCapital = "Hartford", City = "Bridgeport" });  
    23.                 states.Add(new State() { StateID = 6, StateName = "Delaware", StateCapital = "Dover", City = "Wilmington" });  
    24.                 states.Add(new State() { StateID = 7, StateName = "Florida", StateCapital = "Tallahassee", City = "Jacksonville" });  
    25.                 states.Add(new State() { StateID = 8, StateName = "Georgia", StateCapital = "Atlanta", City = "Atlanta" });  
    26.                 states.Add(new State() { StateID = 9, StateName = "Illinois", StateCapital = "Springfield", City = "Chicago" });  
    27.                 states.Add(new State() { StateID = 10, StateName = "Indiana", StateCapital = "Indianapolis", City = "Indianapolis" });  
    28.                 states.Add(new State() { StateID = 11, StateName = "Kansas", StateCapital = "Topeka", City = "Wichita" });  
    29.                 states.Add(new State() { StateID = 12, StateName = "Kentucky", StateCapital = "Frankfort", City = "New Orleans" });  
    30.                 states.Add(new State() { StateID = 13, StateName = "Maryland", StateCapital = "Annapolis", City = "Annapolis" });  
    31.                 states.Add(new State() { StateID = 14, StateName = "Massachusetts", StateCapital = "Boston", City = "Boston" });  
    32.                 states.Add(new State() { StateID = 15, StateName = "Oklahoma", StateCapital = "Oklahoma City", City = "Oklahoma City" });  
    33.             }  
    34.             return states;  
    35.         }  
    36.     }  
    37. }  
  7. Add new Action Method inside the HomeController name :”Learn” and call respository and its method.
    1. public ActionResult Learn()  
    2.        {  
    3.            return View(StateRepository.GetStates());  
    4.        }  
  8. Right click on Action method and add new view for Learn action method.



    Learn.cshtml file will look, as shown below.
    1. @model IEnumerable<DraggableGrid.Models.State>  
    2.   
    3. @{  
    4.     ViewBag.Title = "Learn";  
    5. }  
    6.   
    7. <h2>Learn</h2>  
    8.   
    9. <p>  
    10.     @Html.ActionLink("Create New""Create")  
    11. </p>  
    12. <table class="table">  
    13.     <tr>  
    14.         <th>  
    15.             @Html.DisplayNameFor(model => model.StateName)  
    16.         </th>  
    17.         <th>  
    18.             @Html.DisplayNameFor(model => model.StateCapital)  
    19.         </th>  
    20.         <th>  
    21.             @Html.DisplayNameFor(model => model.City)  
    22.         </th>  
    23.         <th></th>  
    24.     </tr>  
    25.   
    26. @foreach (var item in Model) {  
    27.     <tr>  
    28.         <td>  
    29.             @Html.DisplayFor(modelItem => item.StateName)  
    30.         </td>  
    31.         <td>  
    32.             @Html.DisplayFor(modelItem => item.StateCapital)  
    33.         </td>  
    34.         <td>  
    35.             @Html.DisplayFor(modelItem => item.City)  
    36.         </td>  
    37.         <td>  
    38.             @Html.ActionLink("Edit""Edit"new { id=item.StateID }) |  
    39.             @Html.ActionLink("Details""Details"new { id=item.StateID }) |  
    40.             @Html.ActionLink("Delete""Delete"new { id=item.StateID })  
    41.         </td>  
    42.     </tr>  
    43. }  
    44.   
    45. </table>  
  9. Add jQuery ui and jQquery datatables plugin and give the reference in cshtml file.
    1. <script src="~/Scripts/jquery-1.10.2.min.js"></script>  
    2. <link href="~/Content/jquery-ui.css" rel="stylesheet" />  
    3. <script src="~/Scripts/jquery-ui.min.js"></script>  
    4. <script src="~/Scripts/jquery.dataTables.min.js"></script>  
    5. <link href="~/Content/jquery.dataTables.min.css" rel="stylesheet" />  
  10. When you add jQuery datatables plugin, then you will have to add a few imges also, which helps you to make your grid sortable. These images will be available in jQquery datatables plugin.


  11. You need to make little changes _Layout.cshtml. Hence additional jQuery and CSS should be loaded.

    1. Proceed, as shown below.
      1. @*@Scripts.Render("~/bundles/jquery")  
      2. @Scripts.Render("~/bundles/bootstrap")  
      3. @RenderSection("scripts", required: false)*@  
    2. Add your Learn Action method in menu bar.
      1. <li>@Html.ActionLink("Learn""Learn""Home")</li>  
  12. Add jQuery code given below in Learn.cshtml.
    1. <script type="text/javascript">  
    2.     $(document).ready(function () {         
    3.         $("#example").dataTable({  
    4.             "stripeClasses": [],  
    5.             aLengthMenu: [10],  
    6.             "pagingType""simple"  
    7.         });  
    8.   
    9.     $('#example tbody').on('click''tr'function () {  
    10.         $(this).toggleClass('selected');  
    11.     });  
    12.   
    13.     $("#example tbody").sortable({  
    14.         revert: true,  
    15.         helper: function (e, item) {  
    16.             if (!item.hasClass('selected')) item.addClass('selected');  
    17.             var elements = $('.selected').not('.ui-sortable-placeholder').clone();  
    18.             var helper = $('<table />');  
    19.             item.siblings('.selected').addClass('hidden');  
    20.             return helper.append(elements);  
    21.         },  
    22.         start: function (e, ui) {  
    23.             var elements = ui.item.siblings('.selected.hidden').not('.ui-sortable-placeholder');  
    24.             ui.item.data('items', elements);  
    25.         },  
    26.         update: function (e, ui) {  
    27.             ui.item.after(ui.item.data("items"));  
    28.         },  
    29.         stop: function (e, ui) {  
    30.             ui.item.siblings('.selected').removeClass('hidden');  
    31.             $('tr.selected').removeClass('selected');  
    32.         }  
    33.     });  
    34. });  
    35. </script>  
  13. Add a few style changes as well in style tag in your Learn page.
    1. <style>  
    2.     table {  
    3.         border-collapse: collapse;  
    4.     }  
    5.     table thead tr {  
    6.         margin-left: 20px;  
    7.     }  
    8.     table tbody tr {  
    9.         border: 1px solid #ccc;  
    10.     }  
    11.     tr-selecting {  
    12.         background: #FECA40;  
    13.     }  
    14.     .selected {  
    15.         background: #ff6a00;  
    16.         color: white;  
    17.         cursor: move;  
    18.     }  
    19.     .dataTables_filter {  
    20.         display: none;  
    21.     }  
    22.     .dataTables_length {  
    23.         display: none !important;  
    24.     }  
    25.     .hidden {  
    26.         display: none;  
    27.     }  
    28. </style>  
  14. After making all the changes in Learn.cshtml file, your Learn.cshtml will look, as shown below.
    1. @model IEnumerable<DraggableGrid.Models.State>  
    2.   
    3. <hr />  
    4. <br />  
    5. <style>  
    6.     table {  
    7.         border-collapse: collapse;  
    8.     }  
    9.     table thead tr {  
    10.         margin-left: 20px;  
    11.     }  
    12.     table tbody tr {  
    13.         border: 1px solid #ccc;  
    14.     }  
    15.     tr-selecting {  
    16.         background: #FECA40;  
    17.     }  
    18.     .selected {  
    19.         background: #ff6a00;  
    20.         color: white;  
    21.         cursor: move;  
    22.     }  
    23.     .dataTables_filter {  
    24.         display: none;  
    25.     }  
    26.     .dataTables_length {  
    27.         display: none !important;  
    28.     }  
    29.     .hidden {  
    30.         display: none;  
    31.     }  
    32. </style>  
    33.   
    34. <script src="~/Scripts/jquery-1.10.2.min.js"></script>  
    35. <link href="~/Content/jquery-ui.css" rel="stylesheet" />  
    36. <script src="~/Scripts/jquery-ui.min.js"></script>  
    37. <script src="~/Scripts/jquery.dataTables.min.js"></script>  
    38. <link href="~/Content/jquery.dataTables.min.css" rel="stylesheet" />  
    39.   
    40. <script type="text/javascript">  
    41.     $(document).ready(function () {         
    42.         $("#example").dataTable({  
    43.             "stripeClasses": [],  
    44.             aLengthMenu: [10],  
    45.             "pagingType""simple"  
    46.         });  
    47.   
    48.     $('#example tbody').on('click''tr'function () {  
    49.         $(this).toggleClass('selected');  
    50.     });  
    51.   
    52.     $("#example tbody").sortable({  
    53.         revert: true,  
    54.         helper: function (e, item) {  
    55.             if (!item.hasClass('selected')) item.addClass('selected');  
    56.             var elements = $('.selected').not('.ui-sortable-placeholder').clone();  
    57.             var helper = $('<table />');  
    58.             item.siblings('.selected').addClass('hidden');  
    59.             return helper.append(elements);  
    60.         },  
    61.         start: function (e, ui) {  
    62.             var elements = ui.item.siblings('.selected.hidden').not('.ui-sortable-placeholder');  
    63.             ui.item.data('items', elements);  
    64.         },  
    65.         update: function (e, ui) {  
    66.             ui.item.after(ui.item.data("items"));  
    67.         },  
    68.         stop: function (e, ui) {  
    69.             ui.item.siblings('.selected').removeClass('hidden');  
    70.             $('tr.selected').removeClass('selected');  
    71.         }  
    72.     });  
    73. });  
    74. </script>  
    75.   
    76. <table id="example" class="table">  
    77.     <thead>  
    78.         <tr>  
    79.             <th>  
    80.                 @Html.DisplayNameFor(model => model.StateID)  
    81.             </th>  
    82.             <th>  
    83.                 @Html.DisplayNameFor(model => model.StateName)  
    84.             </th>  
    85.             <th>  
    86.                 @Html.DisplayNameFor(model => model.StateCapital)  
    87.             </th>  
    88.             <th>  
    89.                 @Html.DisplayNameFor(model => model.City)  
    90.             </th>  
    91.         </tr>  
    92.     </thead>  
    93.     <tbody>  
    94.         @foreach (var item in Model)  
    95.         {  
    96.             <tr>  
    97.                 <td>  
    98.                     @Html.DisplayFor(modelItem => item.StateID)  
    99.                 </td>  
    100.                 <td>  
    101.                     @Html.DisplayFor(modelItem => item.StateName)  
    102.                 </td>  
    103.                 <td>  
    104.                     @Html.DisplayFor(modelItem => item.StateCapital)  
    105.                 </td>  
    106.                 <td>  
    107.                     @Html.DisplayFor(modelItem => item.City)  
    108.                 </td>  
    109.             </tr>  
    110.         }  
    111.     </tbody>  
    112. </table>  
  15. Now, we are done. Just press F5 and click Learn tab in your home screen.


  16. Now, select single or multiple rows in the grid. Drag & drop within the grid anywhere.


  17. Click Next and Previous button to do pagination and click header of Grid to sort.

Up Next
    Ebook Download
    View all
    Learn
    View all