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

[Solved] Dynamic view with grid ...

5 Answers 237 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.
Pablo
Top achievements
Rank 1
Pablo asked on 19 Aug 2011, 04:55 PM
Hi,

I'm using a telerik grid in a dynamic view.

Basically I have many different classes with the same field names but different behaviour, so i cannot strongly-type my view (Well i could create many different views but it's too much work) and i am using a dynamic view.

However I'm get the following error when I try to specity any of the columns in the grid:
Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type


I attach the code of my view:
@model dynamic
 
@(Html.Telerik().Grid(Model.PensionPayment)
        .Name("PensionPaymentsGrid")
        .Localizable("en-GB")
        .DataKeys(keys => keys.Add(c => c.EntryID))
            .ClientEvents(events => events.OnDataBound("onSA100MultipleTotalsGridBound").OnEdit("onEditPensionPayment"))
        .DataBinding(dataBinding =>
        {
            dataBinding.Ajax()
                .Update("_AjaxInsertUpdateSA100GridData", "TaxForm", new { pobID = (int)ViewData["pobID"], dialogType = (string)ViewData["dialogType"] })
                .Insert("_AjaxInsertUpdateSA100GridData", "TaxForm", new { pobID = (int)ViewData["pobID"], dialogType = (string)ViewData["dialogType"] })
                .Delete("_AjaxDeleteSA100GridData", "TaxForm", new { pobID = (int)ViewData["pobID"], dialogType = (string)ViewData["dialogType"] })
                .Select("_AjaxGetSA100GridData", "TaxForm", new { pobID = (int)ViewData["pobID"], dialogType = (string)ViewData["dialogType"] });
        })
        .ToolBar(commands =>
        {
            commands.Insert().ButtonType(GridButtonType.Image);
        })
        
        .Columns(columns =>
        {
            columns.Bound(o => o.EntryID).Width(100).Hidden();
            columns.Bound(o => o.ProviderName).Width(100);
            if ((string)ViewData["dialogType"] == "OverseasPensionPayments") { columns.Bound(o => o.Country).Width(100); }
            columns.Bound(o => o.ContractNumber).Width(100);
            if ((string)ViewData["dialogType"] == "PensionPayments") { columns.Bound(o => o.NetPayment).Width(100); }
 
            columns.Bound(o => o.GrossAmount).Width(100).Format("{0:c}");
            columns.Command(commands =>
            {
                commands.Delete().ButtonType(GridButtonType.Image);
                commands.Edit().ButtonType(GridButtonType.Image);
            }).Width(180).Title("");
        })
        .Editable(edit => edit.Mode(GridEditMode.InLine))
        .Scrollable(scrolling => scrolling.Enabled(true))
        .Sortable()
        .Selectable()
        .Pageable(paging => paging.Enabled(false))
        .Filterable()
        .Footer(false))
        <br />

Am I doing something wrong or is it impossible to do what i want with the telerik grid?

Thanks!

5 Answers, 1 is accepted

Sort by
0
Rosen
Telerik team
answered on 22 Aug 2011, 03:28 PM
Hi Pablo,

When using dynamic you should use column's bound method overload which accepts string as field name. You may check this code library.

All the best,
Rosen
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

0
Pablo
Top achievements
Rank 1
answered on 22 Aug 2011, 03:48 PM
Hi Rosen,

thanks for your reply but it doesn't work for me as my view is not strongly-typed to a collection of dynamic objects. It's strongly-typed to a dynamic object which contains a collection.

As long as I try to define my grid
@(Html.Telerik().Grid(Model.PensionPayment)

I cannot use lambda expressions on my grid to define events or commands, because the model itself is dynamic and i cannot cast it. The data collection which is bound to the grid seems to need to be a pure type.

Any ideas?
0
Rosen
Telerik team
answered on 22 Aug 2011, 03:57 PM
Hello Pablo,

As the grid component could only by bound to a collection you should explicitly cast the collection to IEnumerable<dynamic> and make sure it is indeed a collection:

@(Html.Telerik().Grid((IEnumerable<dynamic>)Model.PensionPayment)

All the best,
Rosen
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

0
Pablo
Top achievements
Rank 1
answered on 22 Aug 2011, 04:13 PM
Hi,

thanks again for your answer, but I'm getting another error in the view now, i attach my code
@model dynamic
 
@(Html.Telerik().Grid((IEnumerable<dynamic>)Model.PensionPayment)
        .Name("PensionPaymentsGrid")
        .Localizable("en-GB")
        .DataKeys(x=>x.Add("EntryID"))
            .ClientEvents(events => events.OnDataBound("onSA100MultipleTotalsGridBound").OnEdit("onEditPensionPayment"))
        .DataBinding(dataBinding =>
        {
            dataBinding.Ajax()
                .Update("_AjaxInsertUpdateSA100GridData", "TaxForm", new { pobID = (int)ViewData["pobID"], dialogType = (string)ViewData["dialogType"] })
                .Insert("_AjaxInsertUpdateSA100GridData", "TaxForm", new { pobID = (int)ViewData["pobID"], dialogType = (string)ViewData["dialogType"] })
                .Delete("_AjaxDeleteSA100GridData", "TaxForm", new { pobID = (int)ViewData["pobID"], dialogType = (string)ViewData["dialogType"] })
                .Select("_AjaxGetSA100GridData", "TaxForm", new { pobID = (int)ViewData["pobID"], dialogType = (string)ViewData["dialogType"] });
        })
        .ToolBar(commands =>
        {
            commands.Insert().ButtonType(GridButtonType.Image);
        })
        
        .Columns(columns =>
        {
            columns.Bound("EntryID").Width(100).Hidden();
            columns.Bound("ProviderName").Width(100);
            if ((string)ViewData["dialogType"] == "OverseasPensionPayments") { columns.Bound("Country").Width(100); }
            columns.Bound("ContractNumber").Width(100);
            if ((string)ViewData["dialogType"] == "PensionPayments") { columns.Bound("NetPayment").Width(100); }
 
            columns.Bound("GrossAmount").Width(100).Format("{0:c}");
            columns.Command(commands =>
            {
                commands.Delete().ButtonType(GridButtonType.Image);
                commands.Edit().ButtonType(GridButtonType.Image);
            }).Width(180).Title("");
        })
        .Editable(edit => edit.Mode(GridEditMode.InLine))
        .Scrollable(scrolling => scrolling.Enabled(true))
        .Sortable()
        .Selectable()
        .Pageable(paging => paging.Enabled(false))
        .Filterable()
        .Footer(false))
        <br />

The error message reads as follows:

Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.


Any ideas/solutions?
0
Rosen
Telerik team
answered on 22 Aug 2011, 04:32 PM
Hi Pablo,

As MVC EditorFor (used by default by our component) will not work with dynamic, you will need to specify editor templates which to be used for editing. In case of InLine edit mode EditorTemplateName should be set for every editable column:

columns.Bound("ProviderName").Width(100).EditorTemplateName("MyEditorTemplate");

, as opposite to InForm or PopUp modes where a single editor can be set as shown in the code library I have mentioned in my earlier  reply.All the best,
Rosen
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

Tags
Grid
Asked by
Pablo
Top achievements
Rank 1
Answers by
Rosen
Telerik team
Pablo
Top achievements
Rank 1
Share this question
or