How To: Telerik RadGrid for ASP.NET AJAX with ASP.NET MVC

by Vladimir Enchev | Comments 6

I've made small example how to use RadGrid for ASP.NET AJAX in Microsoft ASP.NET MVC:

Untitled

 

The key here is to inherit from RadGrid and call explicitly desired grid commands. Let's say you want to edit particular record:

1) Create a template column and add this to the ItemTemplate:

<%# Html.ActionLink("Edit", "RadGridCommand", new { ControlID = MyGrid1.ID, CommandName = "Edit", CommandArgument = Container.ItemIndexHierarchical }) %>

 

2) Now handle this in your controller:

public ActionResult RadGridCommand(string ControlID, string CommandName, string CommandArgument)
{
    ViewData["ControlID"] = ControlID;
    ViewData["CommandName"]= CommandName;
    ViewData["CommandArgument"] = CommandArgument;

    return View("Index");
}

 

3) Override OnPreRender in inherited class and call explicitly the grid RaisePostBackEvent() method:

protected override void OnPreRender(EventArgs e)
{
    base.OnPreRender(e);

    if (ViewData["ControlID"] != null && ViewData["ControlID"].ToString() == ID &&
        ViewData["CommandName"] != null)
    {
        RaisePostBackEvent(String.Format("FireCommand:{0};{1};{2}", MasterTableView.UniqueID, (string)ViewData["CommandName"], (string)ViewData["CommandArgument"]));
    }
}

For other useful techniques please refer to the attached source code.

Enjoy!

[Download]

,
Senior Technical Architect

6 Comments

Mike
Man, this can be a great kickstarter for MVC. Who would want an app, without a great looking rich AJAX grid component, be it developed using MVC or whatever-new-cool-technology. Thanks!
jafin
Nice work, can't wait for you guys to get all your tools over to mvc.
John
This example violates the MVC as it uses a LinqDataSource. The latter skips the "M" and the "C" part of MVC by updating the database from the View. Just my 2 cents.
Vlad
John, I have used LinqDataSource for simplicity. You can use pure LINQ to SQL classes instead and not handle any grid commands in the view.
Paul
Please could you show an example of your radGrid providing filtered columns using MVC
Vlad
Hi Paul, You can accomplish this easily using client-side data-binding. Please check this post: http://blogs.telerik.com/VladimirEnchev/Posts/08-10-02/Telerik_RadControls_in_Microsoft_ASP_NET_MVC.aspx

Comments

  1.    
      
      
       
  2. (optional, emails won't be shown on public pages)
  3. (optional)
Read more articles by Vladimir Enchev - or - read latest articles in Developer Tools