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

[Solved] Posting an object to custom toolbar command

3 Answers 150 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.
Stephen
Top achievements
Rank 1
Stephen asked on 04 Apr 2013, 12:50 AM
I have a custom toolbar command (for exporting the data), and the implementation is very similar to the example on the telerik demo site. Because I have custom binding and am only returning some of the data to the grid at one time, I need to get the current sorting / filtering state of the grid. Ideally I would like to post a GridCommand to the action, but it keeps just setting the sorting and filtering to size of 0 and setting the pagesize to 10 (mine is 20) and page number to 1. I can post a stringified JSON of the sort descriptor (looks like this): "{"title":"Test Run","member":"TestRunTestCaseXref.TestRun.TestRunName","type":"String","order":"asc"}"....
That works fine when I make the parameter of the controller action a string, but when I make it a SorterGuy (yes, I will change the name), the variable is always null. 

For the record, I have multiple grids inside a tabview, and all but one of them are loaded into the tabstrip through ajax calls using an addtab function in a telerik.extensions.js file I found on the internet. It shouldn't matter, but I have had trouble sometimes getting those grids to work like the first one. All my testing is being done on the first grid right now though. 

SorterGuy and the controller action look like this:
public class SorterGuy
{
    public string title{get; set;}
    public string member{get; set;}
    public string type{get; set;}
    public string order{get;set;}
}
 
[GridAction]
public ActionResult ExportToolGrid(SorterGuy aval) //SorterGuy is null unless I make it a string.
{
    RouteValueDictionary rvd = this.GridRouteValues();
       //I only get one key here, pagesize... it is the right value though.
    foreach (string key in rvd.Keys)
    {
        object val = rvd[key];
        int i = 0;
    }
 
    MemoryStream output = new MemoryStream();
    StreamWriter writer = new StreamWriter(output, Encoding.UTF8);
    writer.WriteLine();
    writer.Flush();
    output.Position = 0;
 
        return File(output, "text/comma-separated-values", "Orders.csv");
}


I have an ajax grid with the following javascript:

function onDataBound()
{
    var data = $('#@((String)ViewData["gridname"])').data('tGrid');
    if (data.sorted.length > 0)
    {
        // Get the export link as jQuery object
        var $exportLink = $('#export' + '@((String)ViewData["gridname"])');
 
        // Get its 'href' attribute - the URL where it would navigate to
        var href = $exportLink.attr('href');
 
        href = href.replace(/aval=(.*)/, 'aval=' + (JSON.stringify(data.sorted[0]) || '~'));
 
        // Update the 'href' attribute
        $exportLink.attr('href', href);
    }
}


My grid code looks like this: 
@(Html.Telerik().Grid(this.Model == null ? BaseLibrary.TestCaseInstance.LoadAll(null, null, null).Take(Convert.ToInt32(ViewData["pagesize"])) : this.Model).Name((String)ViewData["gridname"]).Columns(columns =>
        {
            columns.Bound(o => o.ResultName).Title("Result").Width(40);
            columns.Bound(o => o.TestRunTestCaseXref.TestRun.TestRunName).Title("Test Run").Width(100);
            columns.Bound(o => o.TestRunTestCaseXref.TestCase.TestCaseName).Title("Test Case").Width(150);
            columns.Bound(o => o.TestRunTestCaseXref.TestRun.TestTool.TestToolName).Title("Tool").Width(40);
            columns.Bound(o => o.TestRunTestCaseXref.TestRun.Package.PackageName).Title("Package").Width(40);
            columns.Bound(o => o.TestRunTestCaseXref.TestRun.TestRunVersion).Title("Version").Width(40);
            columns.Bound(o => o.RunDateTime).Title("Run Date Time").Width(80);
        })
        .DataBinding(databinding =>
        {
            databinding.Ajax().Select("GetToolGridData", "UI", new { gridname = (string)ViewData["gridname"] });
        })
        .EnableCustomBinding(true)
        .ToolBar(commands => commands
            .Custom().HtmlAttributes(new { id = "export" + (String)ViewData["gridname"] })
            .Text("Export to CSV").Action("ExportToolGrid", "UI", new { aval = "~" })
        )
        .Resizable(resizing => resizing.Columns(true))
        .Scrollable(c => c.Height("450px"))
        .Sortable()
        .Pageable(settings => settings.Total((int)ViewData["total"]).PageSize(Convert.ToInt32(ViewData["pagesize"])))
        .Filterable()
        .Footer(true)
        .ClientEvents(events => events.OnRowDataBound("onRowDataBound").OnDataBound("onDataBound"))
)


3 Answers, 1 is accepted

Sort by
0
Stephen
Top achievements
Rank 1
answered on 06 Apr 2013, 09:57 PM
Well, I've solved my own problem, but it doesn't look like I can use the Telerik toolbar. I am going to generate the html myself with the right classes for styling and then use a form POST to download the file (since GET doesn't seem to be able to post complex objects and ajax can't download files). 
0
Steven
Top achievements
Rank 1
answered on 18 Apr 2013, 09:28 PM
Do you know how I can call a javascript method to set a parameter value in the .Action tag for example

  .ToolBar(commands => commands
               .Custom()
                .HtmlAttributes(new { id = "exportgrouphosts" })
                 .Text("Export ")
                .Action("ExportDashboardCsv", "SystemView", new { groupid = JAVASCIPT
METHOD, groupName = " ",  alarmChart = 3 }))

How would I could the groupid = ?
0
Stephen
Top achievements
Rank 1
answered on 19 Apr 2013, 02:06 AM
@Steven: Go here http://demos.telerik.com/aspnet-mvc/razor/grid/customcommand

It does the same thing that you want but sets the page, orderby, and filterby. 

.Action("ExportCsv", "Grid", new { page = 1, orderBy = "~", filter = "~" })


<script type="text/javascript">
    function onDataBound() {
        var grid = $(this).data('tGrid');
         
        // Get the export link as jQuery object
        var $exportLink = $('#export');
         
        // Get its 'href' attribute - the URL where it would navigate to
        var href = $exportLink.attr('href');
         
        // Update the 'page' parameter with the grid's current page
        href = href.replace(/page=([^&]*)/, 'page=' + grid.currentPage);
         
        // Update the 'orderBy' parameter with the grids' current sort state
        href = href.replace(/orderBy=([^&]*)/, 'orderBy=' + (grid.orderBy || '~'));
         
        // Update the 'filter' parameter with the grids' current filtering state
        href = href.replace(/filter=(.*)/, 'filter=' + (grid.filterBy || '~'));
         
        // Update the 'href' attribute
        $exportLink.attr('href', href);
    }
</script>

Tags
Grid
Asked by
Stephen
Top achievements
Rank 1
Answers by
Stephen
Top achievements
Rank 1
Steven
Top achievements
Rank 1
Share this question
or