This question is locked. New answers and comments are not allowed.
Hello,
I try to create a "generic" partial view, using dynamic model and presenting it in a Telerik grid. After lot of troubles I manage to make it work for most of the needed functionality, however I got in a dead end with the ForeignKey columns, it seems that the ForeignKey column can be bound to entitity property only with lambda expression and not with property name as the normal columns, and also seems that expression trees does not work with dynamic model.
Is there any way to deal with ForeignKey + dynamic model?
Is there any technical reason that stops you to implement ForeignKey with property name also? This is pretty severe limitation for me, I am forced to create tens of partial views beause I can't use the ForeignKey feature with dynamic.
In case the ForeignKey Column is not going to work, can anyone suggest a way to handle the columns editable with DropDownList?
I have found a way with custom EditorTemplate, but my solution has a problem with grouping after the foreign key column - there will appear the ID in the title of the group and not the display value... If anyone has a properly working solution I would appreciate help.(I also can share my solution if anyone is intereseted - with the limit that I mentioned above).
Thank you for any suggestion.
Vasile
I try to create a "generic" partial view, using dynamic model and presenting it in a Telerik grid. After lot of troubles I manage to make it work for most of the needed functionality, however I got in a dead end with the ForeignKey columns, it seems that the ForeignKey column can be bound to entitity property only with lambda expression and not with property name as the normal columns, and also seems that expression trees does not work with dynamic model.
Is there any way to deal with ForeignKey + dynamic model?
Is there any technical reason that stops you to implement ForeignKey with property name also? This is pretty severe limitation for me, I am forced to create tens of partial views beause I can't use the ForeignKey feature with dynamic.
In case the ForeignKey Column is not going to work, can anyone suggest a way to handle the columns editable with DropDownList?
I have found a way with custom EditorTemplate, but my solution has a problem with grouping after the foreign key column - there will appear the ID in the title of the group and not the display value... If anyone has a properly working solution I would appreciate help.(I also can share my solution if anyone is intereseted - with the limit that I mentioned above).
Thank you for any suggestion.
Vasile
6 Answers, 1 is accepted
0
Dadv
Top achievements
Rank 1
answered on 06 Mar 2012, 05:01 PM
hi,
Share the sample , i'll take a look.
Share the sample , i'll take a look.
0
Dadv
Top achievements
Rank 1
answered on 07 Mar 2012, 06:47 PM
Like often, when you don't find something, you can make it your self :
And the use in the grid :
it's a first try of the extender, you need the type of the model and of the FK, but i think that it would be easy to remove this necessity.
Regards,
public static class TelerikExtensions { public static GridBoundColumnBuilder<TModel> ForeignKey<TModel,TValue>(this GridColumnFactory<TModel> factory, string member, IEnumerable data, string dataFieldValue, string dataFieldText) where TModel : class { var lambda = ExpressionBuilder.Lambda<TModel>(member); GridForeignKeyColumn<TModel, TValue> column = new GridForeignKeyColumn<TModel, TValue>(factory.Container, (Expression<Func<TModel, TValue>>)lambda, new SelectList(data, dataFieldValue, dataFieldText)); column.Data = new SelectList(data, dataFieldValue, dataFieldText); factory.Container.Columns.Add(column); return new GridBoundColumnBuilder<TModel>(column); } }And the use in the grid :
<%= Html.Telerik().Grid<ClientEditableOrder>() .Name("Grid") .DataKeys(keys => { keys.Add(o => o.OrderID); }) .DataBinding(dataBinding => { dataBinding.Ajax() .Select("_ForeignKeyColumn", "Grid") .Update("_ForeignKeyColumnUpdateOrder", "Grid"); }) .Columns(columns => { columns.Bound(o => o.OrderID).Width(100); //columns.ForeignKey(c=>c.EmployeeID, (IEnumerable)ViewData["employees"], "ID", "Name").Width(230); columns.ForeignKey<ClientEditableOrder,int?>("EmployeeID", (IEnumerable)ViewData["employees"], "ID", "Name").Width(230); columns.Bound(o => o.OrderDate).Width(150); columns.Bound(o => o.Freight).Width(220); columns.Command(commands => commands.Edit()).Width(200); }) .Filterable() .Pageable() %>it's a first try of the extender, you need the type of the model and of the FK, but i think that it would be easy to remove this necessity.
Regards,
0
Dadv
Top achievements
Rank 1
answered on 07 Mar 2012, 08:22 PM
Here the new version :
Now it's like you want i think.
Regards,
public static class TelerikExtensions { public static GridBoundColumnBuilder<TModel> ForeignKey<TModel>(this GridColumnFactory<TModel> factory, string member, IEnumerable data, string dataFieldValue, string dataFieldText) where TModel : class { var lambda = ExpressionBuilder.Lambda<TModel>(member); var genType = typeof(GridForeignKeyColumn<,>).MakeGenericType(typeof(TModel),lambda.ReturnType); var genFunc = typeof(Func<,>).MakeGenericType(typeof(TModel), lambda.ReturnType); var genExp = typeof(Expression<>).MakeGenericType(genFunc); var container = Expression.Constant(factory.Container); var selectList = Expression.Constant(new SelectList(data, dataFieldValue, dataFieldText)); var newConst = Expression.New(genType.GetConstructor(new Type[] { typeof(Grid<TModel>), genExp, typeof(SelectList) }), container, lambda, selectList); IGridForeignKeyColumn column = (IGridForeignKeyColumn)Expression.Lambda(newConst).Compile().DynamicInvoke(); column.Data = new SelectList(data, dataFieldValue, dataFieldText); factory.Container.Columns.Add((GridColumnBase<TModel>)column); return new GridBoundColumnBuilder<TModel>((IGridForeignKeyColumn)column); } }Now it's like you want i think.
Regards,
0
Vasile
Top achievements
Rank 1
answered on 08 Mar 2012, 10:10 AM
Hello Dadv,
Appreciate your feedback on this matter, unfortunatelly it still does not solve the problem with dynamic model.
I have created a test project where I show what I want to accomplish I have added also your extension, that will throw an exception that the object does not contain a property with the name I provide. Your extension work properly when I set proper type in the template parameter, but when it is dynamic that I want to use, will still not work.
I will try to study the source code from telerik, but to be hones I am not very experimented with lambda expressions so far, so will take me some time to manage this, if possible at all.
Thanks,
Vasile
Appreciate your feedback on this matter, unfortunatelly it still does not solve the problem with dynamic model.
I have created a test project where I show what I want to accomplish I have added also your extension, that will throw an exception that the object does not contain a property with the name I provide. Your extension work properly when I set proper type in the template parameter, but when it is dynamic that I want to use, will still not work.
I will try to study the source code from telerik, but to be hones I am not very experimented with lambda expressions so far, so will take me some time to manage this, if possible at all.
Thanks,
Vasile
0
0
Milos
Top achievements
Rank 1
answered on 02 Oct 2012, 07:51 PM
Hello,
Your previous example is exactly what I need, and I followed it step by step, and changed it a little bit (only using SelectList directly from argument):
But, now, I cannot use it, because I cannot access it in my views, there are only two regular overrides there, not this one.
Am I doing something wrong?
Please help me resolve this.
Regards.
Your previous example is exactly what I need, and I followed it step by step, and changed it a little bit (only using SelectList directly from argument):
public static GridBoundColumnBuilder<TModel> ForeignKey<TModel>(this GridColumnFactory<TModel> factory, string member, SelectList select) where TModel : class { var lambda = ExpressionBuilder.Lambda<TModel>(member); var genType = typeof(GridForeignKeyColumn<,>).MakeGenericType(typeof(TModel), lambda.ReturnType); var genFunc = typeof(Func<,>).MakeGenericType(typeof(TModel), lambda.ReturnType); var genExp = typeof(Expression<>).MakeGenericType(genFunc); var container = Expression.Constant(factory.Container); var selectList = Expression.Constant(select); var newConst = Expression.New(genType.GetConstructor(new Type[] { typeof(Grid<TModel>), genExp, typeof(SelectList) }), container, lambda, selectList); IGridForeignKeyColumn column = (IGridForeignKeyColumn)Expression.Lambda(newConst).Compile().DynamicInvoke(); column.Data = select; factory.Container.Columns.Add((GridColumnBase<TModel>)column); return new GridBoundColumnBuilder<TModel>((IGridForeignKeyColumn)column); }But, now, I cannot use it, because I cannot access it in my views, there are only two regular overrides there, not this one.
Am I doing something wrong?
Please help me resolve this.
Regards.