Saturday, August 10, 2019

Selecting / Deselecting all CheckBoxes inside a Webgrid in asp.net MVC4 Application

Step - 1 : Create New Project.

Go to File > New > Project > Select asp.net MVC4 web application > Entry Application Name > Click OK > Select Internet Application > Select view engine Razor > OK

Step-2: Add a Database.

Go to Solution Explorer > Right Click on App_Data folder > Add > New item > Select SQL Server Database Under Data > Enter Database name > Add.

Step-3: Create table for fetch data.

Open Database > Right Click on Table > Add New Table > Add Columns > Save > Enter table name > Ok.

In this example, I have used one tables as below



Step-4: Add Entity Data Model.

Go to Solution Explorer > Right Click on Project name form Solution Explorer > Add > New item > Select ADO.net Entity Data Model under data > Enter model name > Add.
A popup window will come (Entity Data Model Wizard) > Select Generate from database > Next >

Chose your data connection > select your database > next > Select tables > enter Model Namespace > Finish.

Step-5: Add a new Controller.

Go to Solution Explorer > Right Click on Controllers folder form Solution Explorer > Add > Controller > Enter Controller name > Select Templete "empty MVC Controller"> Add.

Step-6: Add new action into your controller for show data in webgrid.

Here I have added "Index" Action into "Webgrid" Controller. Please write this following code

  1. public ActionResult Index()
  2. {
  3. //Here MyDatabaseEntities is our datacontext
  4. List<CustomerInfo> allCust = new List<CustomerInfo>();
  5. using (MyDatabaseEntities dc = new MyDatabaseEntities())
  6. {
  7. allCust = dc.CustomerInfoes.OrderBy(a => a.CustomerID).ToList();
  8. }
  9. return View(allCust);
  10. }

Step-7: Add view for the Action & design.

Right Click on Action Method (here right click on form action) > Add View... > Enter View Name > Select View Engine (Razor) > Check "Create a strong-typed view" > Select your model class > Add.
[N:B:Please Rebuild solution before add view.] 
View 
  1. @model IEnumerable<MVCSelectDeselectAllWebgrid.CustomerInfo>
  2.  
  3. @{
  4. ViewBag.Title = "Index";
  5. WebGrid grid = new WebGrid(source: Model, canPage: false);
  6. }
  7.  
  8. @* Here I am going to add some css for better looks *@
  9.  
  10. <style type="text/css">
  11. table.gridtable {
  12. font-family: verdana,arial,sans-serif;
  13. font-size:11px;
  14. color:#333333;
  15. border-width: 1px;
  16. border-color: #666666;
  17. border-collapse: collapse;
  18. }
  19. table.gridtable th {
  20. border-width: 1px;
  21. padding: 8px;
  22. border-style: solid;
  23. border-color: #666666;
  24. background-color: #dedede;
  25. }
  26. table.gridtable td {
  27. border-width: 1px;
  28. padding: 8px;
  29. border-style: solid;
  30. border-color: #666666;
  31. background-color: #ffffff;
  32. }
  33. </style>
  34.  
  35. <h2>Customer Data</h2>
  36.  
  37. @using (Html.BeginForm("Index","Webgrid", FormMethod.Post))
  38. {
  39. <div>
  40. @grid.GetHtml(
  41. tableStyle: "gridtable",
  42. htmlAttributes: new { id = "checkableGrid" },
  43. columns: grid.Columns
  44. (
  45. //Here I am going to add checkbox column
  46. grid.Column(
  47. format: @<text> <input type="checkbox" value="@item.CustomerID" name="ids" /> </text>,
  48. header: "{checkall}"
  49. ),
  50. grid.Column("CustomerID", "Customer ID"),
  51. grid.Column("CustomerName", "Customer Name"),
  52. grid.Column("Address", "Address"),
  53. grid.Column("City", "City"),
  54. grid.Column("PostalCode", "Postal Code")
  55. )
  56. )
  57. </div>
  58. <div>
  59. <input type="submit" value="Get Selected Record" />
  60. </div>
  61. }
  62.  
  63. @if (ViewBag.Message != null)
  64. {
  65. <div>
  66. @ViewBag.Message
  67. </div>
  68. }
  69.  
  70. @* Here I am going to add some Jquery Code *@
  71.  
  72. @section Scripts{
  73. <script>
  74. $(document).ready(function () {
  75. // 1st replace first column header text with checkbox
  76.  
  77. $("#checkableGrid th").each(function () {
  78. if ($.trim($(this).text().toString().toLowerCase()) === "{checkall}") {
  79. $(this).text('');
  80. $("<input/>", { type: "checkbox", id: "cbSelectAll", value: "" }).appendTo($(this));
  81. $(this).append("<span>Select All</span>");
  82. }
  83. });
  84.  
  85. //2nd click event for header checkbox for select /deselect all
  86. $("#cbSelectAll").live("click", function () {
  87. var ischecked = this.checked;
  88. $('#checkableGrid').find("input:checkbox").each(function () {
  89. this.checked = ischecked;
  90. });
  91. });
  92.  
  93.  
  94. //3rd click event for checkbox of each row
  95. $("input[name='ids']").click(function () {
  96. var totalRows = $("#checkableGrid td :checkbox").length;
  97. var checked = $("#checkableGrid td :checkbox:checked").length;
  98.  
  99. if (checked == totalRows) {
  100. $("#checkableGrid").find("input:checkbox").each(function () {
  101. this.checked = true;
  102. });
  103. }
  104. else {
  105. $("#cbSelectAll").removeAttr("checked");
  106. }
  107. });
  108.  
  109. });
  110. </script>
  111. }
  112.  
  113.  

Step-8: Add another action for POST data.


  1. [HttpPost]
  2. public ActionResult Index(string[] ids)
  3. {
  4. List<CustomerInfo> allCust = new List<CustomerInfo>();
  5. using (MyDatabaseEntities dc = new MyDatabaseEntities())
  6. {
  7. allCust = dc.CustomerInfoes.OrderBy(a => a.CustomerID).ToList();
  8. }
  9.  
  10. // In the real application you can ids
  11.  
  12. if (ids != null)
  13. {
  14. ViewBag.Message = "You have selected following Customer ID(s):" + string.Join(", ", ids);
  15. }
  16. else
  17. {
  18. ViewBag.Message = "No record selected";
  19. }
  20.  
  21. return View(allCust);
  22. }
Step-9: Run Application.

No comments:

Post a Comment

How to register multiple implementations of the same interface in Asp.Net Core?

 Problem: I have services that are derived from the same interface. public interface IService { } public class ServiceA : IService { ...