I've noticed a behavior which, hopefully, there is a workaround for.
When working with a dynamic object as a model for a grid item, it seems to work PERFECT for SERVER-side binding. However, I am using custom binding with a method decorated with the GridAction attribute, and for this, the binding does not seem to be working correctly.
I've noticed that the results in the [data] of the json returned to the client are not populated with the dynamic properties.
Should we somehow be handling this ourselves our should the call to
return View(new GridModel { Data = data, Total = count });be doing it for us?
Thanks!
9 Answers, 1 is accepted
If I have understood your scenario correctly, you requirement is to return different columns during ajax requests? If this is the case, I'm afraid that this is not supported. As mentioned in this help article during ajax binding the actual view is not processed, therefore the columns cannot be changed.
Regards,Rosen
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Basically this is the full scenario:
The application that I am developing supports configurable columns defined in an xml file. From the columns defined in the xml file I can define them in the server side binding doing something like the following:
Columns = (columns => { foreach (var column in columnsDefinedInXml) { columns.Bound(column).Width(100); }}Now, the model for this grid derives from DynamicObject,and the properties associated with the columns defined above are all dynamic.
something like the following:
public class GridItem : DynamicObject{ [ ...]}Now when I create the results in the controller (both server side, and ajax), I do the following:
dynamic dynamicResult = new GridItem();dynamicResult.<name of one of the columns> = somevalue;and send a list of the these as the result.
If I create an IEnumerable<GridItem> filled with data by setting the corresponding dynamic field, it works great when I do the first server-side binding. But if I do any databinding via ajax using my custom binding (where I do the same as for the server-side binding), I notice the generated JSON objects for the results do not contain the information of the dynamic properties.
I hope I clarified things a little! :)
I also noticed in the method GridColumnFactory,Bound (called for server-side binding), you assign a lamba expression to fetch the information for the binding. I assume this is how we can get the dynamic information for server-side.
However, for ajax binding we pass through the method GridActionAttribute.OnActionExecuted where there is no fetching the dynamic information of the results. Would it be in this method that something is missing to populate the json result with the dynamic information?
The observed behavior is caused by the fact that JavaScriptSerializer, used internally to serialize data to JSON, seems to not support DynamicObject serialization. Therefore, in order to support this you should consider implementing a custom JavaScriptConverter and custom JsonResult in order grid to use the converter. I have attached a sample project which illustrates a basic implementation of this.
All the best,Rosen
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
I actually found an alternative... I marked the _container inside the DynamicObject with a public get, and it gets serialized to JSON. Then, inside the column definitions, I added a client template which will fetch the information inside the dictionary on the client side.
e.g:
foreach (var column in columnsToAdd) { columns.Bound(typeof(string), column).Width(100) .ClientTemplate(String.Format(CultureInfo.InvariantCulture, "<#= DynamicProperties['{0}'] #>", column)); }Using my alternative approach, (updated version below):
foreach (var column in columnsToAdd) { columns.Bound(column.ValueType, column.Name).Width(100) .ClientTemplate(String.Format(CultureInfo.InvariantCulture, "<#= DynamicProperties['{0}'] #>", column.Name)); }where the ValueType of the column is DateTime. It would seem the grid does not convert the date time format correctly. I still get something like "/Date(1312473409793)/" shown in the grid. Am I supposed modify my client template accordingly for datetime columns being used this way?
Note: Columns where I bind the data directly without going through the clienttemplate to retreive the value are working perfect. Just the ones that I have to retrieve in my dictionary are causing me problems. Thanks!
I'm not sure what may be the exact cause for the behavior you have described. Therefore, could you please provide a small sample in which it can be observed?
Greetings,
Rosen
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Thanks!
I would also like to use this feature for dynamic object ajax binding. May I know when will be included in future release?
Thanks!
As I have mentioned in my previous message this is an issue with JavaScriptSerializer which will not serialize dynamic objects. We do not have immediate plans to incorporate workaround in our code base, as this is not general solution and may not work in all scenarios. Therefore, you should consider applying the suggested workaround of this JavaScriptSerializer limitation in your own project.
Regards,Rosen
the Telerik team