Inroduction
We have created a simple application to help to bind the data in a Grid using the properties. This article helps to bind the data on the Grid and apply the sorting or paging in an ASP.NET MVC 3 application. The ASP.NET MVC Framework is a web application framework that implements the model-view-controller (MVC) pattern. Based on ASP.NET it allows software developers to build a Web application as a composition of three roles Model, View and Controller. A model represents the state of a particular aspect of the application. Frequently a model maps to a database table with the entries in the table representing the state of the application. A controller handles interactions and updates the model to reflect a change in state of the application and then passes information to the view. A view accepts necessary information from the controller and renders a user interface to display that. We create an instance of Grid with data source as our Model object and we specified that the default sort is FirstName for the sorting purpose and rowsPerPage specified for the paging functionality. The GetHtml method of the Grid object will render an HTML table for the grid helper.
Step 1 : Open Visual Studio 2010.
- Go to file -> New->Projects.
- Create an ASP.NET MVC 3 Web Application.
- Name of "WebGrid".



Step 2 : Add a class in the model folder.
- Right click on the Models folder ->add new items->add class.
- Name of Class is "Employee".
- In a class define the properties.


Step 3 : Define the properties in Employee
class.
Code
using
System;
using
System.Collections.Generic;
using
System.Linq;
using System.Web;
namespace
Webgrid.Models
{
public class
Employee
{
public string
FirstName { get; set;
}
public string
LastName { get; set;
}
public double
Salary { get; set;
}
public static
List<Employee>
GetList()
{
List<Employee>
Employees = new List<Employee>{
new
Employee { FirstName="Rahul",
LastName="Kumar", Salary=45000},
new
Employee { FirstName="Jose",
LastName="Mathews", Salary=25000},
new
Employee { FirstName="Ajith",
LastName="Kumar", Salary=25000},
new
Employee { FirstName="Scott",
LastName="Allen", Salary=35000},
new
Employee { FirstName="Abhishek",
LastName="Nair", Salary=125000}
};
return Employees;
}
}
}
Step 4 :
Add a controller.
- Right click on the Controllers folder ->add->Controllers.
- Name of Controllers is "Home".
- In a controller, define the request.


Step 5 : Write the code on controller.
Code
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.Mvc;
using
Webgrid.Models;
namespace
Webgrid.Controllers
{
public class
HomeController :
Controller
{
public
ActionResult Index()
{
ViewBag.Message = "Welcome to ASP.NET
MVC!";
return View();
}
public
ActionResult About()
{
return View();
}
public
ActionResult show()
{
var empoyees =
Employee.GetList();
return View(empoyees);
}
}
}
Step 6 : Add the view.
- Right click of index methods->add view
- The name of view is "index".


Step 7 : Write the code on view.
Code
@model
Webgrid.Models.Employee
@{
Layout =
null;
}
@{
var grid = new
WebGrid(source: Model,
defaultSort: "FirstName",
rowsPerPage: 3);
}
<h2>Employee
List</h2>
<div
id="grid">
@grid.GetHtml(
tableStyle: "grid",
headerStyle: "head",
alternatingRowStyle: "alt",
columns: grid.Columns(
grid.Column("FirstName"),
grid.Column("LastName"),
grid.Column("Salary",format:@<text>$@item.Salary</text>)
)
)
</div>
Step 8 : Press Crtl+F5 to run application.
Output:


SwethaPosted Dec 5, 2016, 4:42 AM
I have followed the post but when i run the application, i'm getting "resource not found" error any solution pls?
Aniket NarvankarPosted Jun 24, 2015, 8:28 AM
@model IEnumerable<MvcApplication3.Models.Employee> @{ ViewBag.Title = "show"; } @{ var grid = new WebGrid ( source:Model, defaultSort:"Name", rowsPerPage:3 ); } <h2>Show</h2> <div id="grid"> @grid.GetHtml( tableStyle: "grid", headerStyle: "head", alternatingRowStyle: "alt", columns:grid.Columns( grid.Column("Name"), grid.Column("LastName"), grid.Column("Salary"), grid.Column("Id",format:@<text>@item.Id</text>) ) ) </div>
Aniket NarvankarPosted Jun 24, 2015, 8:28 AM
@Prateek Duggal do this this is the entire code on view
RanoPosted Jul 5, 2013, 12:05 AM
good one. there is another good sample in below link http://articlesforprogramming.blogspot.in/2013/07/webgrid-in-aspnet-mvc.html
siva krishnaPosted Jun 17, 2013, 7:47 AM
how can i display data table format without using entity frame work
siva krishnaPosted Jun 17, 2013, 7:46 AM
How can i display
tina raoPosted Mar 22, 2013, 8:07 AM
there is an error in project post that u need to take an enumerable <> tag there in grid design see below @model IEnumerable<Webgrid.Models.Employee> @{ Layout = null; } @{ var grid = new WebGrid(source: Model, defaultSort: "FirstName", rowsPerPage: 3); } <h2>Employee List</h2> <div id="grid"> @grid.GetHtml( tableStyle: "grid", headerStyle: "head", alternatingRowStyle: "alt", columns: grid.Columns( grid.Column("FirstName"), grid.Column("LastName"), grid.Column("Salary",format:@<text>[email protected]</text>) ) ) </div> That's it thanks for the Simple Post for Beginers Regards Trups! JSK
Sudhir SharmaPosted Feb 8, 2013, 5:42 AM
Its not binding data
kasireddy pPosted Nov 9, 2012, 4:30 AM
how can i implement sorting in this gridview
subba reddyPosted Oct 10, 2012, 8:26 AM
WebGrid is third party control i think,we can not directly use. Pls let us the third party control that you have used.
Giri DasariPosted Jul 18, 2012, 2:59 AM
var grid = new WebGrid(source: Model,defaultSort: "FirstName",rowsPerPage: 3);gives error that webgrid is a namespace used as a type
Prateek DuggalPosted Jul 16, 2012, 4:14 AM
var grid = new WebGrid(source: Model, defaultSort: "FirstName", rowsPerPage: 3); gives error that webgrid is a namespace used as a type