Saturday, August 10, 2019

How to delete multiple webgrid rows by using Checkboxes in asp.net MVC 4 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 "List" Action into "Customer" Controller. Please write this following code 

  1. public ActionResult List()
  2. {
  3. List<CustomerInfo> allCust = new List<CustomerInfo>();
  4. // Here MyDatabaseEntities is our Data Context
  5. using (MyDatabaseEntities dc = new MyDatabaseEntities())
  6. {
  7. allCust = dc.CustomerInfoes.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.] 
Complete View 
  1. @model IEnumerable<MVCMultipleDelete.CustomerInfo>
  2. @{
  3. ViewBag.Title = "List";
  4. var grid = new WebGrid(source: Model, rowsPerPage: 10);
  5. }
  6.  
  7. @* Here I will add some css for look webgrid good *@
  8. <style type="text/css">
  9. table.gridtable {
  10. font-family: verdana,arial,sans-serif;
  11. font-size:11px;
  12. color:#333333;
  13. border-width: 1px;
  14. border-color: #666666;
  15. border-collapse: collapse;
  16. }
  17. table.gridtable th {
  18. border-width: 1px;
  19. padding: 8px;
  20. border-style: solid;
  21. border-color: #666666;
  22. background-color: #dedede;
  23. }
  24. table.gridtable td {
  25. border-width: 1px;
  26. padding: 8px;
  27. border-style: solid;
  28. border-color: #666666;
  29. background-color: #ffffff;
  30. }
  31. </style>
  32.  
  33.  
  34. <div style="font-weight:bold;">
  35. Customer List
  36. </div>
  37.  
  38. @using (Html.BeginForm("DeleteSelected","Customer", FormMethod.Post))
  39. {
  40. @grid.GetHtml(
  41. tableStyle:"gridtable",
  42. columns:
  43. grid.Columns(
  44. @*Here I Will Add Checkbox Column*@
  45. grid.Column(format:@<text><input type="checkbox" name="ids" value="@item.CustomerID" /></text>, header:"Select"),
  46. grid.Column("CustomerID","Customer ID"),
  47. grid.Column("CustomerName", "Customer Name"),
  48. grid.Column("Address","Address"),
  49. grid.Column("City","City"),
  50. grid.Column("PostalCode", "Postal Code")
  51. )
  52. )
  53.  
  54. <input type="submit" value="Delete Selected" />
  55. }
  56.  

Step-8: Add another action into your controller for Delete multiple rows at once.

Here I have added "DeleteSelected" Action into "Customer" Controller. Please write this following code 

  1. public ActionResult DeleteSelected(string[] ids)
  2. {
  3. //Delete Selected
  4. int[] id = null;
  5. if (ids != null)
  6. {
  7. id = new int[ids.Length];
  8. int j = 0;
  9. foreach (string i in ids)
  10. {
  11. int.TryParse(i, out id[j++]);
  12. }
  13. }
  14.  
  15. if (id != null && id.Length > 0)
  16. {
  17. List<CustomerInfo> allSelected = new List<CustomerInfo>();
  18. using (MyDatabaseEntities dc = new MyDatabaseEntities())
  19. {
  20. allSelected = dc.CustomerInfoes.Where(a => id.Contains(a.CustomerID)).ToList();
  21. foreach (var i in allSelected)
  22. {
  23. dc.CustomerInfoes.Remove(i);
  24. }
  25. dc.SaveChanges();
  26. }
  27. }
  28. return RedirectToAction("List");
  29. }
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 { ...