This topic shows how to use the Grid UI component in an ASP.NET MVC application.
Important |
|---|
|
All Telerik UI components need a ScriptRegistrar component in order to output their JavaScript objects and register
their JavaScript files. The ScriptRegistrar component
should be defined *after* all other UI components in the page.
If you create the components on the client-side and do not use the MVC extensions,
you need to manually register all requried JavaScript files. For further information check
this help topic (Step 4 - Register Scripts)
|
Prerequisites
Before proceeding make sure that:
-
You have all the required components installed.
-
Make sure that your ASP.NET MVC project refers the Telerik.Web.Mvc.dll
assembly and you have a ScriptRegistrar and a StyleSheetRegistrar defined in your application (either in the master page
or in the view where you are going to use the Grid UI component).
-
All required JavaScript and CSS files are properly copied to your project. This is outlined in here - steps 4 and 5.
Grid Declaration and Binding
Important |
|---|
|
Linq to Sql and the Northwind database are used throughout the Grid documentation for databinding
unless a different database/model is specified. The tutorials require basic Linq to Sql knowledge
and assume that a working Northwind datacontext has been set up beforehand.
|
The following example shows how to bind the Grid UI component to the Orders table of the Northwind database. The default
template for a new ASP.NET MVC application is used.
-
Modify the Index.aspx and make it a strongly typed view where the
model is IEnumerable<Order>. The Order
type is generated by adding the Orders table of the Northwind datacontext.
CopyXML
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage<IEnumerable<Order>>" %> Note |
|---|
|
Having a strongly typed view is not mandatory but makes the Grid declaration easier.
|
-
Modify the Index action method of the HomeController
to supply the Orders as the model of the view.
CopyC#
public ActionResult Index()
{
ViewData["Message"] = "Welcome to ASP.NET MVC!";
NorthwindDataContext northwind = new NorthwindDataContext();
return View(northwind.Orders);
} -
Paste the following code in Index.aspx.
CopyC#
<%= Html.Telerik().Grid(Model)
.Name("OrdersGrid")
.Columns(columns =>
{
columns.Bound(o => o.OrderID);
columns.Bound(o => o.ShipAddress);
columns.Bound(o => o.RequiredDate);
})
.Pageable()
.Sortable()
%> -
Build and run your ASP.NET MVC application. It should show the following:
Now lets explain the Grid declaration code:
See Also