Saturday, August 10, 2019

How to Create Nested WebGrid with Expand/Collapse in ASP.NET MVC

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 a 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 two 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 class for creating a view model.

1st: Add a folder.
Go to Solution Explorer > Right Click on the project > add > new folder.
2nd: Add a class to that folder
Go to Solution Explorer > Right Click on that folder > Add > Class... > Enter Class name > Add.
write the following code in this class

  1. using System.Collections.Generic;
  2.  
  3. namespace MVCNestedWebgrid.ViewModel
  4. {
  5. public class OrderVM
  6. {
  7. public OrderMaster order { get; set; }
  8. public List<OrderDetail> orderDetails { get; set; }
  9. }
  10. }

Step-6: 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-7: Add new action into your controller for show nested data in a webgrid.

Here I have added "List" Action into "Order" Controller. Please write this following code

  1. public ActionResult List()
  2. {
  3. List<OrderVM> allOrder = new List<OrderVM>();
  4.  
  5. // here MyDatabaseEntities is our data context
  6. using (MyDatabaseEntities dc = new MyDatabaseEntities())
  7. {
  8. var o = dc.OrderMasters.OrderByDescending(a => a.OrderID);
  9. foreach (var i in o)
  10. {
  11. var od = dc.OrderDetails.Where(a => a.OrderID.Equals(i.OrderID)).ToList();
  12. allOrder.Add(new OrderVM { order= i, orderDetails = od });
  13. }
  14. }
  15. return View(allOrder);
  16. }

Step-8: 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.] 
Html Code 
  1. @model IEnumerable<MVCNestedWebgrid.ViewModel.OrderVM>
  2.  
  3. @{
  4. ViewBag.Title = "Order List";
  5. WebGrid grid = new WebGrid(source: Model, canSort: false);
  6. }
  7.  
  8.  
  9. <div id="main" style="padding:25px; background-color:white;">
  10. @grid.GetHtml(
  11. htmlAttributes: new {id="gridT", width="700px" },
  12. columns:grid.Columns(
  13. grid.Column("order.OrderID","Order ID"),
  14. grid.Column(header:"Order Date",format:(item)=> string.Format("{0:dd-MM-yyyy}",item.order.OrderDate)),
  15. grid.Column("order.CustomerName","Customer Name"),
  16. grid.Column("order.CustomerAddress","Address"),
  17. grid.Column(format:(item)=>{
  18. WebGrid subGrid = new WebGrid(source: item.orderDetails);
  19. return subGrid.GetHtml(
  20. htmlAttributes: new { id="subT" },
  21. columns:subGrid.Columns(
  22. subGrid.Column("Product","Product"),
  23. subGrid.Column("Quantity", "Quantity"),
  24. subGrid.Column("Rate", "Rate"),
  25. subGrid.Column("Amount", "Amount")
  26. )
  27. );
  28. })
  29. )
  30. )
  31. </div>
Css Code 
  1. <style>
  2.  
  3. th, td {
  4. padding:5px;
  5. }
  6. th
  7. {
  8. background-color:rgb(248, 248, 248);
  9. }
  10. #gridT, #gridT tr {
  11. border:1px solid #0D857B;
  12. }
  13. #subT,#subT tr {
  14. border:1px solid #f3f3f3;
  15. }
  16. #subT {
  17. margin:0px 0px 0px 10px;
  18. padding:5px;
  19. width:95%;
  20. }
  21. #subT th {
  22. font-size:12px;
  23. }
  24. .hoverEff {
  25. cursor:pointer;
  26. }
  27. .hoverEff:hover {
  28. background-color:rgb(248, 242, 242);
  29. }
  30. .expand {
  31. background-image: url(/Images/pm.png);
  32. background-position-x: -22px;
  33. background-repeat:no-repeat;
  34. }
  35. .collapse {
  36. background-image: url(/Images/pm.png);
  37. background-position-x: -2px;
  38. background-repeat:no-repeat;
  39. }
  40.  
  41. </style>
Write the following Jquery code for make webgrid collapsible 
  1. <script>
  2. $(document).ready(function () {
  3. var size = $("#main #gridT > thead > tr >th").size(); // get total column
  4. $("#main #gridT > thead > tr >th").last().remove(); // remove last column
  5. $("#main #gridT > thead > tr").prepend("<th></th>"); // add one column at first for collapsible column
  6. $("#main #gridT > tbody > tr").each(function (i, el) {
  7. $(this).prepend(
  8. $("<td></td>")
  9. .addClass("expand")
  10. .addClass("hoverEff")
  11. .attr('title',"click for show/hide")
  12. );
  13.  
  14. //Now get sub table from last column and add this to the next new added row
  15. var table = $("table", this).parent().html();
  16. //add new row with this subtable
  17. $(this).after("<tr><td></td><td style='padding:5px; margin:0px;' colspan='" + (size - 1) + "'>" + table + "</td></tr>");
  18. $("table", this).parent().remove();
  19. // ADD CLICK EVENT FOR MAKE COLLAPSIBLE
  20. $(".hoverEff", this).live("click", function () {
  21. $(this).parent().closest("tr").next().slideToggle(100);
  22. $(this).toggleClass("expand collapse");
  23. });
  24. });
  25.  
  26. //by default make all subgrid in collapse mode
  27. $("#main #gridT > tbody > tr td.expand").each(function (i, el) {
  28. $(this).toggleClass("expand collapse");
  29. $(this).parent().closest("tr").next().slideToggle(100);
  30. });
  31. });
  32. </script>
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 { ...