ARTICLE

Disply Data on Grid in ASP.NET MVC 3 Application

Posted by Manish Singh Articles | ASP.NET MVC with C# December 27, 2011
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.
Reader Level:

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".

start.gif

strtttt.gif

interstart.gif

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.

addclass.gif

ccccccccccccccccccccc.gif

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.

controller.gif

controname.gif

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".

view.gif

viewsssss.gif

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:

output.gif

Login to add your contents and source code to this article
Article Extensions
Contents added by shailaja s on Dec 18, 2012
post comment
     

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>$@item.Salary</text>))) </div> That's it thanks for the Simple Post for Beginers Regards Trups! JSK

Posted by tina rao Mar 22, 2013

Its not binding data

Posted by Sudhir Sharma Feb 08, 2013

how can i implement sorting in this gridview

Posted by kasireddy p Nov 09, 2012

WebGrid is third party control i think,we can not directly use. Pls let us the third party control that you have used.

Posted by subba reddy Oct 10, 2012

var grid = new WebGrid(source: Model,defaultSort: "FirstName",rowsPerPage: 3);gives error that webgrid is a namespace used as a type

Posted by Giri Dasari Jul 18, 2012
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
Get Career Advice from Experts
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Get Career Advice from Experts