Kendo UI Grid Wrapper for ASP.NET MVC – Getting Started

Overview:

In this blog post I will be exploring how to get started with our Kendo UI Grid Wrapper for ASP.NET MVC. We will take a basic usage of displaying data in a grid format in an ASP.NET MVC view. To know more about Kendo UI Web controls head over to www.kendoui.com. If you want to follow along with this blog post, you can do so by downloading a 30 day free trial of Kendo UI Complete for ASP.NET MVC.

Getting Started:

First lets create a ASP.NET MVC project. I am using Visual Studio 2012. When you install Kendo UI, we also install certain project templates which make it easy to create kendo UI based ASP.NET MVC project. I assuming you have installed Kendo UI Web for the rest of the post. In Visual Studio, select File > New Project > Web > Kendo UI for ASP.NET MVC. Give it a name and wait for Visual Studio to spin up the new project.

Datasource:

For the sake of this blog post, I will be using Northwind database. I will also be using ADO.NET Entity Model to Northwind database and use Customers table data. So go ahead and add a ADO.NET Entity Model to your project. We will use the entity data model to fetch data and bind it to the grid. I have also created a CustomerViewModel so that I can use this as the Model to the view. Here is the code for the CustomerViewModel:


public class CustomerViewModel
{
public string CustomerID { get; set; }
public string CompanyName { get; set; }
public string ContactName { get; set; }
public string ContactTitle { get; set; }
public string Country { get; set; }
}

Home Controller & Index Action method changes:

We will read the customers data, build CustomerViewModel list and add this to view as a Model. Here is the Index Action method code snippet:


public ActionResult Index()
{
var customerViewModelList = GetCustomers();
return View(customerViewModelList);
}

GetCustomers is a helper method which converts Customers entity to CustomerViewModel entity and return a list. Here is the code snippet of GetCustomers method:


private IEnumerable<CustomerViewModel> GetCustomers()
{
var context = new NORTHWINDEntities();
var customers = context.Customers.Select(customer => new CustomerViewModel
{
CustomerID = customer.CustomerID,
CompanyName = customer.CompanyName,
ContactName = customer.ContactName,
ContactTitle = customer.ContactTitle,
Country = customer.Country,
});
return customers;
}

Adding Grid to View:

In order to use a grid on a view, you will use the GridBuilder. GridBuilder supports the following overloaded methods for creating the grid:


public virtual GridBuilder<T> Grid<T>() where T : class;

public virtual GridBuilder<DataRowView> Grid(DataTable dataSource);

public virtual GridBuilder<DataRowView> Grid(DataView dataSource);

public virtual GridBuilder<T> Grid<T>(IEnumerable<T> dataSource) where T : class;

public virtual GridBuilder<T> Grid<T>(string dataSourceViewDataKey) where T : class;

in this blog post we will be using the method where it allows us to pass a IEnumerable as the data source. First lets take a look at the code snippet. Following the snippet I will explain the code in detail:


@using GridGettingStarted.Models
@model IEnumerable<CustomerViewModel>
@(Html.Kendo().Grid(Model)
.Name("Grid")
.Columns(columns =>
{
columns.Bound(c => c.ContactName).Width(140);
columns.Bound(c => c.ContactTitle).Width(190);
columns.Bound(c => c.CompanyName);
columns.Bound(c => c.Country).Width(110);
})
.Pageable()
.Sortable()
.Groupable()
.Filterable()
)

Well that’s all it takes to create a grid in MVC. Now lets go over the code in detail.

  • We pass the Model which is IEnumerable<CustomerViewModel> in this case to the GridBuilder
  • All widgets in Kendo UI needs to be named, so we provide a name using the Name() method
  • We define columns of the grid using the Columns() method. We can provide lambda expression to indicate which columns should be bound
  • In order to allow paging on the grid, we just add the Pageable() method
  • Sorting on the grid is enabled by adding the Sorting() method
  • Grouping is enabled by adding the Groupable() method
  • Similarly, filtering is provided by adding the Filterable() method

Now build the project and run the application. Following is the output of the code we just wrote:

image

Server Operation False:

At this moment if you click on a pager link or try to sort or try to group, Kendo UI Grid will be default send a request back to Server. That’s the default behavior that the grid works with. It assumes that Server would like to perform operation before getting the data. In this scenario that is not intended for me. I have got all the data that I need and I have given it to the grid. So it should do all the operation at client side rather than sending a request to server. For this to happen we just need to let the Grid DataSource not to perform server operation. This can be done by setting the Grid DataSource setting as  below:


@(Html.Kendo().Grid(Model)
.Name("Grid")
.Columns(columns =>
{
columns.Bound(c => c.ContactName).Width(140);
columns.Bound(c => c.ContactTitle).Width(190);
columns.Bound(c => c.CompanyName);
columns.Bound(c => c.Country).Width(110);
})
.Pageable()
.Sortable()
.Groupable()
.Filterable()
.DataSource(source => source
.Ajax()
.ServerOperation(false)
)
)

now when you run the project and perform any operations, grid will not send a request to the server but do it client side.

Conclusion:

This blog post was like a primer to anybody who would like to get started with Kendo UI Grid Wrapper for ASP.NET MVC. With just 15 lines of settings you will have a full fledged grid in your app within no time. If you are interested in Kendo UI Grid, do download a 30 day free trial at www.kendoui.com.

Advertisements

5 thoughts on “Kendo UI Grid Wrapper for ASP.NET MVC – Getting Started

  1. Hi,

    I have used the code and is working fine. I added .Editable() method and is working fine, it is updating the record too but pagination is not working as I made the grid to editable.

    Here is my code:

    @(Html.Kendo().Grid(Model)
    .Name(“Grid”)
    .Columns(columns =>
    {
    columns.Bound(c => c.ProductId).Width(20);
    columns.Bound(c => c.ProductName).Width(100);
    columns.Bound(c => c.UnitPrice).Width(50);
    columns.Bound(c => c.UnitsInStock).Width(40);
    columns.Bound(c => c.Discontinued).Width(30);

    @* Add title of columns*@
    //columns.Bound(c => c.Discontinued).Title(“Test”);

    // Add ing a columne make row editable.
    // This will add the command buttons in last column.
    columns.Command(command => command.Edit()).Width(100).Title(“Action”);
    })
    .Pageable(pager => pager.PageSizes(new[] { 5, 10, 15 }))
    .Sortable()
    .Groupable()
    .Filterable()

    .Editable(editable => editable.Mode(GridEditMode.InLine))
    .DataSource(datasource => datasource
    .Ajax()
    .Model(model => model.Id(product => product.ProductName))
    .Update(update => update.Action(“UpdateProduct”, “Home”))
    .Read(read => read.Action(“actionmethod”, “controller”)))
    )

    Model:

    public class Product
    {
    public string ProductId { get; set; }
    public string ProductName { get; set; }
    public string UnitPrice { get; set; }
    public string UnitsInStock { get; set; }
    public string Discontinued { get; set; }
    }

    How can I fix the pagination issue?

    Thanks in advance!

  2. Your explanation for client side paging and the settings for datasource -> serveroperation(false) to enable client side paging, solved my problem with paging.

    thanks,
    -t

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s