Telerik ASP.NET Ajax RadGrid in MVC and ViewState?

2019-07-24 17:06发布

问题:

That's right, an ASP.NET Ajax control in MVC. I know I know, there are Telerik MVC controls but the Telerik MVC grid doesn't have everything I need so I'm dead set on using the ASP.NET Ajax control.

Anyways, I have the RadGrid up and running great. The problem occurs when I enable all the bells and whistles such as paging, sorting, filtering etc. It looks like it puts the group by/filter/sort data in an eventargument post variable:

__EVENTARGUMENT  FireCommand:2$RadGrid1$ctl00;GroupByColumn;Dialog

In my case, when the page reloads, nothing changes. Is there something I should enable for this to work in MVC? I have followed the instructions at http://www.telerik.com/help/aspnet-ajax/mvc-getting-started.html but this doesn't come up. The example in the previous url doesn't have all the bells and whistles, so I'm assuming there are additional steps I need to take to get this to work.

Thanks!

回答1:

There is also a Ajax Grid component developed by Stephen Halter at Microsoft, it's more simple than Telerik RadComponent but it's extensible and it offers pagin, sorting, edit, create new, etc. You can download this component from NuGet:

PM> Install-Package AjaxGridScaffolder


回答2:

One document that I always look into when dealing with integrating the RadControls for ASP.NET AJAX into ASP.NET MVC is the Limitations article, which actually specifically mentions that the built-in sorting/grouping/filtering (anything that causes a postback), is not supported in ASP.NET MVC. However, there is some hope :) It does link to this blog post which has a solution that contains some workarounds all of this, which should be helpful here. The post is a bit old, but I think you can still get something helpful out from it.

As a side-note here there might be some ninja ways to work with the Telerik MVC Grid so that you can get all your requirements and have the benefit of going native ASP.NET MVC. Perhaps post them on the Telerik forums?



回答3:

I was able to find a solution to this, though it's not very pretty. It uses a little reflection and hard coded mapping to tree of objects. Hopefully this will be a good starting point for anyone needing ViewState in MVC.

Basically it involves deserializing the ViewState into an object then using reflection, call the Control's LoadViewState with the right branch in the object's tree.

    string viewState = Request.Form["__VIEWSTATE"];

    if (!string.IsNullOrEmpty(viewState))
    {
        LosFormatter formatter = new LosFormatter();

        object savedStateObject = formatter.Deserialize(viewState);

        MethodInfo method = grid.GetType().GetMethod("LoadViewState", BindingFlags.NonPublic | BindingFlags.Instance);

        // TODO: Find a less brittle/more elegant way to isolate the appropiate viewstate object for this control
        // In the case of Telerik's RadGrid, the key wasy find the tree that had an array of 13 objects
        method.Invoke(grid, new object[] { (((((((((savedStateObject as Pair).First as Pair).Second as Pair).Second as System.Collections.ArrayList)[1] as Pair).Second as System.Collections.ArrayList)[1] as Pair).Second as System.Collections.ArrayList)[1] as Pair).First });
    }

    string eventArgument = Request.Form["__EVENTARGUMENT"];

    if (!string.IsNullOrEmpty(eventArgument))
    {
        grid.RaisePostBackEvent(eventArgument);
    }

See this post for more details: Supporting ViewState in an MVC ViewUserControl



回答4:

Check out this documentation from Telerik, it should have what you need to get this working: Integrating RadControls in ASPNET MVC