WebGrid in ASP.Net MVC 4 to Show the Content in Grid Format

Introduction

This article explains how to use the WebGrid in ASP.NET MVC 4.

Step 1

First of all add a new application in ASP.NET MVC 4 and name it "Web Grid in MVC".

webgrid1.jpg

Now add a new class to the Models Folder and name it "Student.cs".

webgrid2.jpg

Step 2

Now we add four properties to the Student Class.

  1. public class Student  
  2. {  
  3.     public string RollNo { getset; }  
  4.     public string Name { getset; }  
  5.     public string Branch { getset; }  
  6.     public long FeeRemaining { getset; }  
  7. }

Here I provide RollNo, Name, Branch and FeeRemaining for the Student Class.

Now we will add a Controller Class to the Controller Folder. For that right-click on the Controller Folder and click on "Add" -> "Controller Class".

webgrid3.jpg

Step 3

Now we will add the values to all the four properties that we had added through the Model Folder.

These values needed to be passed in the Controller Class that we recently added.

  1. {  
  2.     ObservableCollection<Student> FeeRemaining = new ObservableCollection<Student>();  
  3.     FeeRemaining.Add(new Student { RollNo = "08330001", Name = "Surbhi", Branch = "C.S", FeeRemaining = 18000 });  
  4.     FeeRemaining.Add(new Student { RollNo = "08330004", Name = "Arun", Branch = "C.S", FeeRemaining = 2500 });  
  5.     FeeRemaining.Add(new Student { RollNo = "08329006", Name = "Ankita", Branch = "I.T", FeeRemaining = 31000 });  
  6.     FeeRemaining.Add(new Student { RollNo = "08329007", Name = "Anshika", Branch = "I.T", FeeRemaining = 9450 });  
  7.     FeeRemaining.Add(new Student { RollNo = "08329014", Name = "Anubhav", Branch = "I.T", FeeRemaining = 2670 });  
  8.     FeeRemaining.Add(new Student { RollNo = "08311023", Name = "Girish", Branch = "E.N", FeeRemaining = 11200 });  
  9.     FeeRemaining.Add(new Student { RollNo = "08311024", Name = "Yogesh", Branch = "E.N", FeeRemaining = 3370 });  
  10.     return View(FeeRemaining);  
  11. } 

Step 4

Now moving to the final part of the application, add a view to the Views Folder by right-clicking on the Views Folder.

webgrid4.jpg

Now we need to add some code to this page.

  1. @{  
  2. var gd = new WebGrid(Model, canPage: true, rowsPerPage: 5, selectionFieldName: "selectedRow",ajaxUpdateContainerId: "gridContent");  
  3.     gd.Pager(WebGridPagerModes.NextPrevious);} 

This is a WebGrid helper and this will allow us to add CSS Classes to the header, footer, table and so on.

Now we will add some CSS to our application, this will be added in the same view but in the head section.

 

  1. <style type="text/css">  
  2.     .table { margin4px;  width500px;  background-color:#FCFCFC;}  
  3.     .head { background-color#C1D4E6font-weightboldcolor#FFF; }  
  4.     .webGrid th, .webGrid td { border1px solid #C0C0C0padding5px; }  
  5.     .altRow { background-color#E4E9F5color#000; }  
  6.     .gridHead a:hover {text-decoration:underline;}  
  7.     .description { width:auto}  
  8.     .selectRow{background-color#389DF5}  
  9. </style>  

 

Step 5

Now we will add the columns to a Grid, for that we need to add the following code:

  1. <div id="gridContent">  
  2. @gd.GetHtml(tableStyle: "table",  
  3.         headerStyle: "head",  
  4.         alternatingRowStyle: "altRow",  
  5.         selectedRowStyle: "selectRow",  
  6.         columns: gd.Columns(  
  7.         gd.Column("RollNo", format: (item) => item.GetSelectLink(item.RollNo)),  
  8.         gd.Column("Name"" Name"),  
  9.         gd.Column("Branch""Branch", style: "description"),  
  10.         gd.Column("FeeRemaining""FeeRemaining")  
  11.  ))

This code will navigate through the selected Roll No; for that we use the "format" parameter as in the following: 

  1. gd.Column("RollNo", format: (item) => item.GetSelectLink(item.RollNo)),  

Step 6

Also we will show the details of the Student in HTML format when the user clicks on the Roll No of the student; for that you need to add the following code:

 

  1. @if (gd.HasSelection)  
  2.      {  
  3.          Student = (WebGridSampleApplication.Models.Student)gd.Rows[gd.SelectedIndex].Value;  
  4.          <b>Roll No</b> @Student.RollNo<br />  
  5.          <b>Name</b>  @Student.Name<br />  
  6.          <b>Branch</b> @Student.Branch<br />  
  7.          <b>Remaining Fee</b> @Student.FeeRemaining<br />  
  8.      }  

 

One main thing is still remaining and it is to add the "jqery-1.7.1.min.js". You can download my code that is available at the begining of this article and from there can add this jQuery to your application.

Output

When you debug your application you will get the details of the Student in the Grid Form.

webgrid5.jpg

Now if you click on the Roll No of any student then these details will be shown at the bottom of the Grid and that's in HTML Format.

webgrid6.jpg


Similar Articles