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

Tuesday, September 30, 2008 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]

6 Comments

  • Mike 30 Sep 2008
    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 01 Oct 2008
    Nice work, can't wait for you guys to get all your tools over to mvc.
  • John 02 Oct 2008
    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 02 Oct 2008
    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 13 Oct 2008
    Please could you show an example of your radGrid providing filtered columns using MVC
  • Vlad 15 Oct 2008
    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

Add comment

  1. Formatting options
       
     
     
     
     
       
  2. (optional, emails won't be shown on public pages)
  3. (optional)