Using ViewModel objects to improve the Ajax performance of Telerik Grid for ASP.NET MVC

Posted by: the telerik blogs, on 11 Nov 2009 | View original | Bookmarked: 0 time(s)

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

<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>
Controller:

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;
}
}

Lets try this setup and go to the second page of the grid. FireBug reports that 6418 bytes have been transferred:

BeforeDto

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. Lets 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;
}
}

It contains only the properties relevant in this particular grid configuration. By using it we will serialize only the important data thus improving the total size of the Ajax request. Here is

the updated configuration:

View:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<UsingDto.Models.OrderViewModel>>" %>

<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>
The type of the model and the ContactName column are changed.

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
};
}
} AfterDto  
gzip compression in IIS7 for the JSON content type:

AfterGzip

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.

UsingDto.zip

I hope this helps!

Advertisement
Category: ASP.NET | Other Posts: View all posts by this blogger | Report as irrelevant | View bloggers stats | Views: 951 | Hits: 14

Similar Posts

  • Re-engineering RadGridView for WinForms (part II) more
  • Silverlight Release History : Q2 2009 (version 2009.2.701) more
  • Telerik Introduces Free Web Testing Framework for ASP.NET AJAX and Silverlight more
  • Introducing grid client-side editing support with Q3 2008 more
  • VS2008 and .NET 3.5 Service Pack 1 Beta now available more
  • Grid Configuration via Web Service more
  • WPF Release History : Q1 2009 (version 2009.1.312) more
  • Nevron .NET Vision 2009 Vol. 1 is now available for download more
  • Grid Configuration via Web Service more
  • Telerik Announces Q3 2007 Release more

News Categories

.NET | Agile | Ajax | Architecture | ASP.NET | BizTalk | C# | Certification | Data | DataGrid | DataSet | Debugger | DotNetNuke | Events | GridView | IIS | Indigo | JavaScript | Mobile | Mono | Patterns and Practices | Performance | Podcast | Refactor | Regex | Security | Sharepoint | Silverlight | Smart Client Applications | Software | SQL | VB.NET | Visual Studio | W3 | WCF | WinFx | WPF | WSE | XAML | XLinq | XML | XSD