简体   繁体   中英

How can I get the data from a Kendo grid selected row when using server binding?

I have a Kendo grid with a button on each row, which I want to trigger a URL action to download a PDF file when clicked. In order to do this I need to pass the data ID to the URL action, but I am having a lot of trouble getting a reference to it. This is my table definition:

@(Html.Kendo().Grid(Model.revisions)
                    .Name("RevisionsGrid")
                    .Columns(columns =>
                    {
                        columns.Bound(p => p.RevisionInfo.RevisionDate).Title("Date Modified");
                        columns.Bound(p => p.RevisionInfo.User.Name).Title("By User");
                        columns.Command(command => command.Custom("ViewPdf").Text("View PDF").Click("getPdf"));
                    })
                    .Sortable()
                    .Selectable()
                    .Events(e => e.Change("selection_change"))
                    .Pageable(p => p.PageSizes(new[] { 5, 10, 25 }))
                    .DataSource(dataSource => dataSource
                    .Server()
                    .Model(model => model.Id(p => p.RevisionInfo.Id)))

I need a reference to the model's RevisionInfo.Id. I do not want to change this to Ajax binding.

I tried the solution here but this.dataItem always returns null. I also tried using a reference to the Kendo grid instead of this with the same result.

function getPdf(e) {
    e.preventDefault();
    var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
    var revisionId= dataItem.RevisionId;

    window.location.href = "@Url.Action("GetPdf","Reports")?revisionId=" + revisionId; 
    }

This seems like it should be simple but I'm at a loss. Any ideas?

You missed the kendo grid element when querying the dataitem:

function getPdf(e) {
   e.preventDefault();
   var dataItem = $("#RevisionsGrid").data("kendoGrid").dataItem($(e.currentTarget).closest("tr"));
   var revisionId= dataItem.RevisionId;

   window.location.href = "@Url.Action("GetPdf","Reports")?revisionId=" + revisionId; 
}

Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM