This is a migrated thread and some comments may be shown as answers.

[Solved] Receiving metadata

5 Answers 118 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
cp
Top achievements
Rank 1
cp asked on 17 Feb 2012, 07:19 PM
Hello,
Is it possible to receive metadata in the onDataBound event handler, just like one can send metadata in the OnDataBinding handler?

I have this case where we use CustomBinding, however I need to return some additional data on top of the collection that gets bound to the grid.

[GridAction(EnableCustomBinding = true)]
        public ActionResult GetNewPage(...)
        {
            [...]
            return View(new GridModel
            {
                Data = ApplyPaging(data, command.Page, command.PageSize),
                Total = data.AsQueryable().Count()
            });
        }


Thanks,

5 Answers, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 22 Feb 2012, 04:29 PM
Hello Claudiu,

Sending additional data is not supported out of the box and will require some additional code. Basically, you should inherit the GridActionAttribute and override the OnActionExecuted and the CreateActionResult methods. The additional data can be obtained on the client from the event argument's response property of the OnComplete client-side event e.g.

public class MyGridActionAttribute: GridActionAttribute
{
    private ActionExecutedContext context;
    //Add the ActionExecutedContext to the attribute in order to get access to the
    // Controller context
    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        this.context = filterContext;
        base.OnActionExecuted(filterContext);
    }
 
    // Add the additional data from the ViewData/ViewBag to the result        
    protected override ActionResult CreateActionResult(object model)
    {
        var dictionary = model as Dictionary<string,object>;
        var data = context.Controller.ViewData["additionalData"];
        dictionary.Add("additionalData", data);
        return base.CreateActionResult(model);
    }
}


Kind regards,
Daniel
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
Alexander
Top achievements
Rank 1
answered on 12 Jun 2012, 11:15 AM
Hi Daniel,

Thanks for the solution. Unfortunately it's not thread safe. Current context field could be overwritten before CreateActionResult is called. I wonder if it's possible to send additional data in a safe way.

Thanks,
Alexander
0
Daniel
Telerik team
answered on 14 Jun 2012, 01:53 PM
Hello Alexander,

I am not sure if I understand correctly in which case the context would be overwritten since there are no static members and the Controller is created for each request. Could you provide a sample scenario in which the problem could occur?

Regards,
Daniel
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the Telerik Extensions for ASP.MET MVC, subscribe to their blog feed now.
0
Alexander
Top achievements
Rank 1
answered on 14 Jun 2012, 02:22 PM
Hi Daniel,

Yes, Controller is created for each request, but GridActionAttribute is not (as far as I understand).

So, when Thread 1 enters OnActionExecuted method, a reference to the current context is stored to a field and base.OnActionExecuted is executed. The base method does some stuff and calls virtual method CreateActionResult where previously stored context is used. If a processor switches to another thread (Thread 2) before CreateActionResult is called on Thread 1 and executes OnActionExecuted on Thread 2, the reference to the context of Thread 1 is rewritten with a context of Thread 2. Thus, both ActionResults have the same additionalData, because both of the threads have the same context.

Please let me know if it's still unclear. I'm really interested in a good solution. Right now I'm using System.Web.HttpContext.Current.Items to pass the data from a controller to a GridActionAttribute, but I wonder if it's possible to get rid of static HttpContext.Current.

Thanks,
Alexander
0
Daniel
Telerik team
answered on 19 Jun 2012, 01:52 PM
Hello again Alexander,

I apologize for missing that the attribute is not created for each request. I am afraid that currently there isn't another way to pass the extra data. An option is to write your own version of the OnActionExecuted method which uses similar code as the GridActionAttribute and supports passing the needed data.

Regards,
Daniel
the Telerik team
Check out the successor of Telerik MVC Extensions - Kendo UI for ASP.NET MVC - and deem it for new ASP.NET MVC development.
Tags
Grid
Asked by
cp
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Alexander
Top achievements
Rank 1
Share this question
or