In this blog post I will demonstrate how to squeeze the most from Telerik Grid for ASP.NET MVC when using Ajax binding.
Lets start with a simple grid bound to the Northwind Orders table:
View:
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<UsingDto.Models.Order>>" %>Controller:
<asp:Content ID="indexTitle" ContentPlaceHolderID="TitleContent" runat="server">
Home Page
</asp:Content>
<asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">
<%= Html.Telerik().Grid(Model)
.Name("Grid")
.Columns(columns =>
{
columns.Add(o => o.OrderID).Width(100);
columns.Add(o => o.Customer.ContactName).Width(200);
columns.Add(o => o.ShipAddress);
columns.Add(o => o.OrderDate).Format("{0:MM/dd/yyyy}").Width(120);
})
.Ajax(ajax => ajax.Action("_AjaxBinding", "Home"))
.Pageable()
.Sortable()
.Scrollable()
.Filterable()
%>
<%= Html.Telerik().ScriptRegistrar() %>
</asp:Content>
public class HomeController : Controller
{
public ActionResult Index()
{
return View(GetOrders());
}
[GridAction]
public ActionResult _AjaxBinding()
{
return View(new GridModel
{
Data = GetOrders()
});
}
private IEnumerable<Order> GetOrders()
{
NorthwindDataContext northwind = new NorthwindDataContext();
return northwind.Orders;
}
}
Let’s try this setup and go to the second page of the grid. FireBug reports that 6418 bytes have been transferred:
This is because all the properties of the Order object are serialized in JSON. Since we have a column which is using the Customer property of the Order all properties of the Customer object are serialized as well. Let’s improve the payload size using the following ViewModel object:
public class OrderViewModel
{
public int OrderID
{
get;
set;
}
public string ContactName
{
get;
set;
}
public string ShipAddress
{
get;
set;
}
public DateTime? OrderDate
{
get;
set;
}
}
the updated configuration:
View:
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<UsingDto.Models.OrderViewModel>>" %>The type of the model and the ContactName column are changed.
<asp:Content ID="indexTitle" ContentPlaceHolderID="TitleContent" runat="server">
Home Page
</asp:Content>
<asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">
<%= Html.Telerik().Grid(Model)
.Name("Grid")
.Columns(columns =>
{
columns.Add(o => o.OrderID).Width(100);
columns.Add(o => o.ContactName).Width(200);
columns.Add(o => o.ShipAddress);
columns.Add(o => o.OrderDate).Format("{0:MM/dd/yyyy}").Width(120);
})
.Ajax(ajax => ajax.Action("_AjaxBinding", "Home"))
.Pageable()
.Sortable()
.Scrollable()
.Filterable()
%>
<%= Html.Telerik().ScriptRegistrar() %>
</asp:Content>
Controller:
public class HomeController : Controller
{
public ActionResult Index()
{
return View(GetOrders());
}
[GridAction]
public ActionResult _AjaxBinding()
{
return View(new GridModel
{
Data = GetOrders()
});
}
private IEnumerable<OrderViewModel> GetOrders()
{
NorthwindDataContext northwind = new NorthwindDataContext();
return from o in northwind.Orders
select new OrderViewModel
{
OrderID = o.OrderID,
ContactName = o.Customer.ContactName,
ShipAddress = o.ShipAddress,
OrderDate = o.OrderDate
};
}
}
GZIP compression introduces two times smaller payload and almost ten times than the initial implementation.
Find attached the sample project used in the blog post.
I hope this helps!