WebGrid Helper in ASP.Net Web API

Introduction

This article describes the WebGrid helper in the ASP.NET Web API. The Web Grid used in HTML table elements show the data in the Web page elements. It creates an environment for presenting the tabular data. The WebGrid supports server-side paging and it enables the custom formatting, such as column formatting, sorting the data, defining how muany rows are displayed in one page (paging) on the tabular data.

Various properties of Web Grid used in this application are the following:

  1. canPage: This property is for paging in the application.
  2. rowsPerPage: This property is for defining the number of rows in one page.
  3. canSort: This property allow the user to sort the table data by clicking on the column heading.

Other properties that are used in the WebGrid:

  1. DefaultSort: By using this property we define how the data can be sort, we provide the name of the column for sorting the data.
  2. Source: It defines where the data comes from.

Now I will show an example of the WebGrid.

Step 1

Create an Web API application:

  • Start Visual Studio 2012.

  • From the start window select "New Project".

  • In the Template Window select "Installed" -> "Visual C#" -> "Web".

  • Select "ASP.NET MVC 4 Web Application" and click on "OK"

    grid.jpg

  • From the "MVC4 Project" window select "Web API".

    grid1.jpg

Step 2

Create a Model class "Customer.cs".

  • In the "Solution Explorer".
  • Right-click on the "Model folder" -> "Add" -> "Class".

    grid2.jpg

  • Select "Class" and click on the "OK" button.

Add the following code:

 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. namespace WebGridApp.Models  
  6. {  
  7.     public class Customer  
  8.     {  
  9.         public string Name { get;set; }  
  10.         public string Address { getset; }  
  11.         public double M_salary { getset; }  
  12.     }  
  13. }  

 

Step 3

Open the "HomeController":

  • In the "SolutionExplorer".
  • Select "Controller folder" -> "HomeController".

    grid3.jpg

Add this code to this controller:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using WebGridApp.Models;  
  7. namespace WebGridApp.Controllers  
  8. {  
  9.     public class HomeController : Controller  
  10.     {  
  11.        public ActionResult Index()  
  12.         {  
  13.             var customers= new List<Customer>  
  14.              {  
  15.                new Customer { Name="Rohit",Address="Delhi", M_salary=40000},  
  16.                new Customer{ Name="Rahul", Address="Kanpu", M_salary=35000},  
  17.                new Customer { Name="Rehan", Address="Luckmow", M_salary=55000},  
  18.                new Customer { Name="Namrata", Address="Agra", M_salary=35000},  
  19.                new Customer { Name="Shivani", Address="Chennai", M_salary=67000},  
  20.                new Customer { Name="Rohit",Address="Noida", M_salary=78000},  
  21.                new Customer{ Name="Smith", Address="Gaziabad",M_salary=67000},  
  22.                new Customer { Name="Manya", Address="Merath",M_salary=100000},  
  23.                new Customer { Name="Shalini", Address="Mumbai",M_salary=130000},  
  24.                new Customer { Name="Priyam", Address="Gujrat", M_salary=90000}  
  25.                    };  
  26.               return View(customers);  
  27.         }  
  28.     }  
  29. } 

Step 4

Create the "Index.cshtml" file as in the following:

  • In the "Solution Explorer".

  • Select "view folder" -> "Home" -> "index.cshtml".

    grid4.jpg

Add this code:

  1. @model IEnumerable<WebGridApp.Models.Customer>  
  2. <html>  
  3. <head>     
  4. <title>Index</title>  
  5.     <style type="text/css">  
  6.         .Grid { margin: 3px; border-collapse: collapse; width: 400px; }  
  7.        .center { background-color: #A8E8E8; font-weight: bolder; color: #ffd800; }   
  8.         .Grid th, .Grid td { border: 2px solid #ffd800; padding: 6px; }   
  9.         .na { background-color: #E8E8E8; color: #f00; }  
  10.          .tech{ width: 200px; font-weight:bolder;}  
  11.     </style>  
  12.  </head>  
  13.  <body>  
  14.     @{  
  15.         var page = new WebGrid(Model, canPage: true, rowsPerPage: 4);  
  16.         page.Pager(WebGridPagerModes.NextPrevious);  
  17.         @page.GetHtml(tableStyle: "Grid",  
  18.                 headerStyle: "center",  
  19.                 alternatingRowStyle: "na",  
  20.                 columns: page.Columns(  
  21.                     page.Column("Name""Name", canSort: true,  
  22.                     format:@<b>@item.Name</b>, style: "tech"),  
  23.                     page.Column("Address""Address", canSort: true),  
  24.                     page.Column("M_salary","M_salary",canSort:true)  
  25.        ));  
  26.      }  
  27.  </body>  
  28. </html> 

Step 5

Execute the application by pressing "F5". The output divides into 3 pages. We can check click on "1,2,3" pages.

p.jpg

p1.jpg

Click on the column heading  "Name". The data will be sorted by name.

p2.jpg

Click on the column heading "Address". The data will be sorted by Address.

p3.jpg

Click on the column heading "M_salary". The data will be sorted by M-salary.

p4.jpg


Similar Articles