ASP.NET MVC Grid View using MVCContrib

In this post, I demonstrate how you can use the  Grid UI helper of the MVCContrib project in your ASP.NET MVC  application. MVCContrib is a community project that adds the functionalities to Microsoft’s ASP.NET MVC Framework and makes the framework easier to use. MVCContrib provides several UI helpers and Grid UI helper is one of them. The Grid helper provides the functionalities of GridView control of ASP.NET GridView. The Grid component generates HTML tables for displaying data from a collection of Model objects and it support paging. The MVCContrib project can download from http://www.CodePlex.com/MvcContrib.

The following are the steps to get Grid to work:

Step1

Add a reference to the MvcContrib assembly (download available from http://www.CodePlex.com/MvcContrib )

Step 2

Add a namespace import for MvcContrib.UI.Html to your web.config file:
<pages>
    <namespaces>
        <add namespace="MvcContrib.UI"/>
        <add namespace="MvcContrib.UI.Html"/>
        <add namespace="MvcContrib.UI.Html.Grid"/>
       <add namespace="MvcContrib"/>
    </namespaces>
</pages>

Using the Grid

 The Grid supports pagination through the AsPagination extension method. For this, you should import the MvcContrib.Pagination namespace in your controller.

Imports the following namespace to your controller class for pagination support

using MvcContrib.Pagination;

 Controller Class
 

public ActionResult List(int? page) {
    
using (DBDataContext db = new DBDataContext()) {
     
ViewData["categories"] = db.Categories.ToList().AsPagination(page ?? 1,10);
      
return View("Categories");
    }

The above action method of the Controller class used the AsPagination extension method. If you don't want pagination, you can  avoid  AsPagination method from Action method. The AsPagination extension method will work on any IEnumerable<T> or IQueryable<T>. This extension method has two arguments and the first one is the page number and the second optional parameter is the size of each page. The default value of the second parameter is 20. The pagination HTML will automatically generated by the Grid.

 View

In your View you can then make a call to Html.Grid.

div>
       
<%
           
Html.Grid<Category>(
               
"categories",
               
column => {
                   
column.For(c => c.CategoryID, "ID");
                   
column.For(c => c.CategoryName);
                   
column.For(c => c.Description);
                   
column.For("Edit").Do(c => { %>
                    
<td><a href="/Category/Edit/<%= c.CategoryID %>">Edit</a>
                     
</td>
                 
<%}); }
          );
      
%>    </div>

 
The first parameter is the ViewData key that contains the Model data and the second parameter is used to construct the column model for the grid. The column.For will generates the columns for the Grid.The lambda expression c => c.CategoryID will generates the value of the property CategoryID and the value of the second parameter "ID" is the custom heading of the column. If custom heading is not specified, the name of the property will also use for heading. You can add custom columns by calling column.For("column name") and then specifying a lambda for generating the HTML. The last column.For method of the above view code will add a custom column name "Edit".

For more details visit  http://www.CodePlex.com/MvcContrib

 

Published Wednesday, July 23, 2008 4:30 AM by shiju
Filed under: ,

Comments

# ASP.NET MVC Archived Blog Posts, Page 1

Thursday, July 24, 2008 1:59 AM by ASP.NET MVC Archived Blog Posts, Page 1

Pingback from  ASP.NET MVC Archived Blog Posts, Page 1

# ASP.NET MVC Validations using MVCContrib

Monday, August 25, 2008 7:26 AM by Shiju Varghese's Blog

In my earlier post , I have explained how to use MVCContrib Grid in ASP.net MVC application. In this

# re: ASP.NET MVC Grid View using MVCContrib

Thursday, September 25, 2008 12:51 PM by Baldev Rawat (baldev.rawat@gmail.com)

Hi Shiju

  thanks. I am able to use the grid in project with the help of this step by step blog.

  Thank u very much

# ASP.NET MVC Beta Released

Thursday, October 16, 2008 4:20 AM by Shiju Varghese's Blog

Microsoft has released the official beta for ASP.NET MVC. You can download the Beta version from here

# re: ASP.NET MVC Grid View using MVCContrib

Thursday, October 16, 2008 12:05 PM by koistya

Good example. Thanks.

# re: ASP.NET MVC Grid View using MVCContrib

Monday, December 29, 2008 2:26 AM by jaydeep_vishwakarma

I am able to use it , But i want place place there alternate rows CSS . Please tell me what i need to do?

# re: ASP.NET MVC Grid View using MVCContrib

Monday, December 29, 2008 9:03 AM by shiju

@jaydeep_vishwakarma

I would like to recommend to use jQuery.  Have a look into the colorcharge.com/.../jquery-alternate-table-rows

# Links interessanti

Friday, January 16, 2009 9:14 PM by OMAA.net patWorld

Links interessanti

# ASP.NET MVC 1.0 Release Candidate (RC) Released

Tuesday, January 27, 2009 10:06 PM by Shiju Varghese's Blog

After 5 CTP versions and 1 Beta version, Microsoft has finally shipped Release Candidate version of ASP

# re: ASP.NET MVC Grid View using MVCContrib

Wednesday, February 18, 2009 8:41 AM by jusufjk

Shiju...

Thx for the step

--------------------

"Do or Do Not, There is No Try"

# ASP.NET MVC Tip: Add a new T4 template for making MVCContrib Grid Helper Component

Wednesday, March 04, 2009 1:49 AM by Shiju Varghese's Blog

In this tip, I demonstrate how you can add a T4 scaffolding template within the “Add View” dialog of

# ASP.NET MVC Tip: Add a new T4 template for making MVCContrib Grid Helper Component

Wednesday, March 04, 2009 2:22 AM by Shiju Varghese's Blog

In this tip, I demonstrate how you can add a T4 scaffolding template within the “Add View” dialog of

# re: ASP.NET MVC Grid View using MVCContrib

Thursday, April 30, 2009 1:58 AM by srorigin

Hi,

Shiju

first of all thanks for this post secondly i want to know how can i use hierarchical grid with this ?

any idea ?

# re: ASP.NET MVC Grid View using MVCContrib

Friday, July 10, 2009 3:11 AM by sunilsingh

Hi I am New to ASP.net MVC and i tried to implement the code but i am getting an error in config file

Configuration Error

Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately.

Parser Error Message: Unrecognized configuration section pages.

Source Error:

Line 34:  </connectionStrings>

Line 35:  

Line 36:     <pages>

Line 37:       <namespaces>

Line 38:         <add namespace="MvcContrib.UI"/>

Can anyone please help me out

# re: ASP.NET MVC Grid View using MVCContrib

Friday, July 10, 2009 7:19 AM by shiju

@sunilsingh,

Check the latest version of MVCContrib grid. The details available from mvccontrib.codeplex.com/.../View.aspx

# re: ASP.NET MVC Grid View using MVCContrib

Tuesday, August 04, 2009 5:18 PM by Jon

The question I have is, is it possible to create an editable DataGrid in ASP.NET MVC like you can with web forms? Or, is the above mostly for view only?

# re: ASP.NET MVC Grid View using MVCContrib

Tuesday, September 22, 2009 2:27 AM by Suresh

In aspx page mentioned  Html.Grid<Category>. What is this <Category> Please tell me.

# re: ASP.NET MVC Grid View using MVCContrib

Friday, October 02, 2009 10:01 AM by kmullings

Thanks Shiju!  This is a BIG time saver.

How can I use this for an inline "Add" function?

Leave a Comment

(required) 
(required) 
(optional)
(required)