Click here to Skip to main content
15,881,139 members
Articles / Web Development / ASP.NET
Tip/Trick

Using the Grid.MVC in ASP.NET MVC

Rate me:
Please Sign up or sign in to vote.
4.68/5 (33 votes)
23 May 2013CPOL2 min read 558.1K   20.9K   62   63
How to use the Grid.MVC in ASP.NET MVC

Introduction 

Most of times we will have to show data in our apps, but we need to provide different features like paging, filtering, sorting and much more, so the Grid.MVC provides amazing tools for that goal when we develop applications using ASP.NET MVC.

Background

This article shows how through the Grid.MVC you can use features like paging, filtering and sorting.

Using the code   

Firts step: create a new ASP.NET MVC 4 application, I recommended to use the basic template.

Next, lets create a new class Client, this class will be the model:

C#
public class Client
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
}

Now that you have the model done, lets create the Client controller. This controller have only on action, and the data source will be a List, but here you can connect to a database, the action returns the data:

C#
public class ClientController : Controller
{
    private readonly List clients = new List()
    {
        new Client { Id = 1, Name = "Julio Avellaneda", Email = "julito_gtu@hotmail.com" },
        new Client { Id = 2, Name = "Juan Torres", Email = "jtorres@hotmail.com" },
        new Client { Id = 3, Name = "Oscar Camacho", Email = "oscar@hotmail.com" },
        new Client { Id = 4, Name = "Gina Urrego", Email = "ginna@hotmail.com" },
        new Client { Id = 5, Name = "Nathalia Ramirez", Email = "natha@hotmail.com" },
        new Client { Id = 6, Name = "Raul Rodriguez", Email = "rodriguez.raul@hotmail.com" },
        new Client { Id = 7, Name = "Johana Espitia", Email = "johana_espitia@hotmail.com" }
    };
 
    public ActionResult Index()
    {
        return View(clients);
    }
} 

Before create the view, will need to add a Grid.MVC package using Nuget:

 Image 1

Also add Boostrap:

Image 2

When the Grid.MVC package is installed, you can found some new views in the Views/Shared folder:

Image 3 

In the scripts folder, tree new JavaScript files will be found:

 Image 4

And in the content folder now you see the css file for the Grid:

Image 5 

Next step now is to create a new View for the action Index of the controller Client, like this: 

C#
@model IEnumerable<Grid.Models.Client>
@using GridMvc.Html
 
@{
    Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <link href="@Url.Content("~/Content/Gridmvc.css")" rel="stylesheet" />
    <link href="@Url.Content("~/Content/bootstrap.min.css")" rel="stylesheet" />
    <script src="@Url.Content("~/Scripts/jquery-1.9.1.min.js")"></script>
    <script src="@Url.Content("~/Scripts/gridmvc.min.js")"></script>
    <title>Index</title>
</head>
<body>
    <h1>Grid.MVC</h1>
    <hr />
    <div style="width:500px;">
        @Html.Grid(Model).Columns(columns => 
                    {
                        columns.Add(c => c.Id).Titled("Client ID");
                        columns.Add(c => c.Name).Titled("Name").Filterable(true);
                        columns.Add(c => c.Email).Titled("Email");
                    }).WithPaging(3).Sortable(true)
    </div>
</body>
</html>

The key parts on the code are:

  • Add: @using GridMvc.Html 
  • Add the references to the css files
  • Add the references to the JavaScript files

To use the Grid, use the HTML Helper @Html.Grid, some important properties for the helper:

  • Titled: The column title
  • Filterable: Define if the column has the feature to be filterable
  • Sortable: Define if the column has the feature to be sortable
  • WithPaging: Define the number of rows to show for page 

Finally, you have the following cool Grid:


Image 6


I hope this post will be useful for you!

Best regards!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) BDotNet
Colombia Colombia
Microsoft ASP.NET MVP, I love software development, especially web development, I have worked with Microsoft technologies for about five years in the development of large scale enterprise applications, co-creator of several Carreras for the Microsoft Virtual Academy (MVA), speaker at events Microsoft Colombia and member of the Core Group BDotNet community. You can see some of my contributions in http://julitogtu.com/

Comments and Discussions

 
AnswerRe: Hi, I'm having a great headache with this. Pin
Amruta Keskar19-Aug-14 20:57
Amruta Keskar19-Aug-14 20:57 
QuestionDisplaying # of items Pin
css294-Apr-14 14:06
css294-Apr-14 14:06 
QuestionHow to refresh the Grid using jquery. Pin
Saurabh Shekhar0524-Mar-14 2:47
Saurabh Shekhar0524-Mar-14 2:47 
Questionsearch textbox and paging? Pin
djramc21-Mar-14 7:28
djramc21-Mar-14 7:28 
QuestionWeb Forms Pin
Luis Oliveira 19661-Mar-14 23:06
Luis Oliveira 19661-Mar-14 23:06 
Questioninner join with grid mvc. Pin
ravikhoda25-Feb-14 20:12
professionalravikhoda25-Feb-14 20:12 
QuestionRetrieve record from the displayed list Pin
Member 105720025-Feb-14 6:55
Member 105720025-Feb-14 6:55 
QuestionHow to add a column for display purpose? Pin
Member 1055601128-Jan-14 9:25
Member 1055601128-Jan-14 9:25 
Hi,

I downloaded the package and liked it very much. It is very straightforward and no need to mess around with HTML. I use it to display a strongly typed class into grid. My question is: How to add a column for display purpose only? Column value is a string. I tried
Columns.Add(stringValue).Titled("colHeader"); and
columns.Add().Encoded(false).Sanitized(false).RenderValueAs(stringValue)

Both did not work.

Thanks very much for help.

Hugh
AnswerRe: How to add a column for display purpose? Pin
Member 84046852-Apr-15 7:50
Member 84046852-Apr-15 7:50 
GeneralMy vote of 5 Pin
Member 1032399325-Jan-14 23:06
professionalMember 1032399325-Jan-14 23:06 
QuestionGrid.MVc Pin
ravikhoda1-Jan-14 0:48
professionalravikhoda1-Jan-14 0:48 
QuestionSQL Database part in controller Pin
jasket12329-Aug-13 4:20
jasket12329-Aug-13 4:20 
AnswerRe: SQL Database part in controller Pin
julitogtu2-Nov-13 8:37
julitogtu2-Nov-13 8:37 
QuestionNeed Help Pin
rocky00519-Aug-13 12:40
rocky00519-Aug-13 12:40 
QuestionActionLink Pin
Member 13432712-Aug-13 13:47
Member 13432712-Aug-13 13:47 
AnswerRe: ActionLink Pin
Arkady Geltzer4-Oct-13 3:32
Arkady Geltzer4-Oct-13 3:32 
GeneralRe: ActionLink Pin
tncchairman12-Dec-13 16:56
tncchairman12-Dec-13 16:56 
QuestionAbout Row Modification Pin
deo cabral5-Aug-13 17:52
professionaldeo cabral5-Aug-13 17:52 
AnswerRe: About Row Modification Pin
julitogtu2-Nov-13 8:42
julitogtu2-Nov-13 8:42 
AnswerRe: About Row Modification Pin
deo cabral14-Nov-13 12:48
professionaldeo cabral14-Nov-13 12:48 
QuestionGrid.MVC scrooling large datasets.. Pin
flyingfishnm4-Jul-13 4:38
flyingfishnm4-Jul-13 4:38 
AnswerRe: Grid.MVC scrooling large datasets.. Pin
julitogtu15-Jul-13 7:13
julitogtu15-Jul-13 7:13 
QuestionRe: Grid.MVC scrooling large datasets.. Pin
LeBarros27-Jul-14 7:19
LeBarros27-Jul-14 7:19 
QuestionBootstrap Pin
John B Oliver6-Jun-13 12:25
John B Oliver6-Jun-13 12:25 
AnswerRe: Bootstrap Pin
julitogtu7-Jun-13 6:38
julitogtu7-Jun-13 6:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.