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

[Solved] Help with GridRouteValues() and a custom template

6 Answers 194 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.
Jeff
Top achievements
Rank 1
Jeff asked on 09 Nov 2011, 12:38 PM
I am currently trying to get Grid State working for paging and filtering working with some of our grids but have an issue.

The demo code I have downloaded for the view is below
columns.Template(p =>
    {
        ViewBag.GridState["id"] = p.ID;
     %>
        <%: Html.ActionLink("Details", "Details", (RouteValueDictionary)ViewBag.GridState) %>
     <%
    });

I am using Razor and a custom template how do I get my parameters ie the id required for the controller action into the ViewBag.Gridstate?

columns.Template(
    @<text>
         @Html.ActionLink("Edit", "Edit", @item.ControllerName, new { id = @item.ID}, null)
         @Html.ActionLink("Delete", "Delete", @item.ControllerName, new { id = @item.ID }, null)
    </text>).Width(40).Title("Commands");
columns.Bound(r => r.Name).Title("Name").Width(200);

We are using @<Text> and @item in this instance as otherwise our actionlinks do not render.

Any help greatly appreciated as this statefulness is the last missing piece to our solution

Thanks

Jeff...

My current template code is

6 Answers, 1 is accepted

Sort by
0
Accepted
Daniel
Telerik team
answered on 10 Nov 2011, 05:27 PM
Hi Jeff,

I attached the demo solution converted into Razor syntax. Hope it helps.


Greetings,
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
Jeff
Top achievements
Rank 1
answered on 11 Nov 2011, 10:22 AM
Hi Daniel,

Thanks so much for the reply, is there any way of stopping the actual parameters showing in the rendered HTML?

For example
@<text>
    @(ViewBag.GridState["sectionID"] = item.SectionID)
    @(ViewBag.GridState["policyZoneID"] = Model.PolicyZoneID)
    @(ViewBag.GridState["policyID"] = Model.PolicyID)
    @(ViewBag.GridState["policyVersion"] = Model.PolicyVersion)
    @Html.ActionLink("Edit", "Edit", @item.ControllerName, (RouteValueDictionary)ViewBag.GridState, null)
    @Html.ActionLink("Delete", "Delete", @item.ControllerName, (RouteValueDictionary)ViewBag.GridState, null)
</text>).Width(40).Title("Commands");
Would render a load of Id's in front of the command buttons, am I going to have use some JQuery glue to hide these or is there a better way I cannot see?

I could do this but is there a better way

<div style="display: none;">@(ViewBag.GridState["sectionID"] = item.SectionID)
@(ViewBag.GridState["policyZoneID"] = Model.PolicyZoneID)
@(ViewBag.GridState["policyID"] = Model.PolicyID)
@(ViewBag.GridState["policyVersion"] = Model.PolicyVersion)</div>

Thanks

Jeff....
0
Accepted
Daniel
Telerik team
answered on 14 Nov 2011, 01:20 PM
Hello Jeff,

To avoid this behavior, you should replace the brackets when assigning the parameters. For example:
@<text>
    @{
        ViewBag.GridState["sectionID"] = item.SectionID;
        ViewBag.GridState["policyZoneID"] = Model.PolicyZoneID;
        ViewBag.GridState["policyID"] = Model.PolicyID;
        ViewBag.GridState["policyVersion"] = Model.PolicyVersion;
    }
      
    @Html.ActionLink("Edit", "Edit", @item.ControllerName, (RouteValueDictionary)ViewBag.GridState, null)
    @Html.ActionLink("Delete", "Delete", @item.ControllerName, (RouteValueDictionary)ViewBag.GridState, null)
</text>).Width(40).Title("Commands");

 
For your convenience I attached the modified project.

All the best,
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
Jeff
Top achievements
Rank 1
answered on 14 Nov 2011, 01:24 PM
Thanks Daniel just the ticket :0)

I know this is cheeky but I have another thread open, how do we do this for Ajax bound clientTemplates???

Thanks Jeff......
0
Daniel
Telerik team
answered on 16 Nov 2011, 03:12 PM
Hi Jeff,

This can`t be done so easily as there is no way to access the GridState from the client template. Basically you will have to pass all the arguments through the query string and then assign them through the Grid`s API.
I attached a sample project which implements this scenario. I hope it helps.

Greetings,
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
Alex
Top achievements
Rank 1
answered on 18 Mar 2012, 11:55 AM

Hello Daniel

 

I just have tested your solution and the paging is not working properly. After the Back it always shows the items from the first page but at the same time the pager shows the correct page number.

I have added this code but it requires one additional call.

Is it possible to avoid it?

 

Thanks,

Alex


function onDataBinding(e) {
        var grid = $(this).data("tGrid");
        if (firstTimeLoading) {
            grid.filterBy = "@Html.Raw(ViewData["filters"] as string)";
            grid.orderBy = "@Html.Raw(ViewData["orderBy"] as string)";
            //grid.currentPage = @(ViewData["page"] ?? 1);
            //firstTimeLoading = false;
        }
    }
  
 function OnDataBound(e) {
        if (firstTimeLoading) {
            var grid = $(this).data("tGrid");
            var index = @(ViewData["page"] ?? 1);
            if(index != grid.currentPage)
                grid.pageTo(@(ViewData["page"] ?? 1));
            firstTimeLoading = false;
        }

Tags
Grid
Asked by
Jeff
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Jeff
Top achievements
Rank 1
Alex
Top achievements
Rank 1
Share this question
or