0

当我尝试将 kendo resault 导出为 pdf 时,我收到此错误“输入字符串的格式不正确。”

这是我的代码:

  @(Html.Kendo().Grid((IEnumerable<A.Models.MyViewModel>)ViewBag.myviewmodelbag) //Bind the grid to ViewBag.Products
  .Name("List")
  .Columns(columns =>
  {
      columns.Bound(myviewmodelbag => myviewmodelbag.BaseData.Type);

      columns.Bound(myviewmodelbag => myviewmodelbag.BaseData.Title);

  })

  .Pageable() // Enable paging
  .Sortable() // Enable sorting
  .Groupable()
  .Filterable()
  .Events(ev => ev.DataBound("onDataBound"))
  .ToolBar(toolBar => 
                toolBar.Custom()
                    .Text("Export To PDF")
                    .HtmlAttributes(new { id = "export" })
                    .Url(Url.Action("Export", "MyViewModel", new { page = 1, pageSize = "~", filter = "~", sort = "~" }))
            )

<script type="text/javascript"> 

    function onDataBound(e) {
        var grid = $('#List').data('kendoGrid');


        // ask the parameterMap to create the request object for you
        var requestObject = (new kendo.data.transports["aspnetmvc-server"]({ prefix: "" }))
        .options.parameterMap({
            page: grid.dataSource.page(),
            sort: grid.dataSource.sort(),
            filter: grid.dataSource.filter()
        });

        // 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=' + requestObject.page || '~');

        // Update the 'sort' parameter with the grid's current sort descriptor
        href = href.replace(/sort=([^&]*)/, 'sort=' + requestObject.sort || '~');

        // Update the 'pageSize' parameter with the grid's current pageSize
        href = href.replace(/pageSize=([^&]*)/, 'pageSize=' + grid.dataSource._pageSize);

        //update filter descriptor with the filters applied

        href = href.replace(/filter=([^&]*)/, 'filter=' + (requestObject.filter || '~'));

        // Update the 'href' attribute
        $exportLink.attr('href', href);
    }

</script>

我的控制器是这样的:

public FileResult Export([DataSourceRequest]DataSourceRequest request)
        {
            IEnumerable products = db.AA.ToDataSourceResult(request).Data;

现在的问题是,如果我像上面那样使用 exatcly,我会得到错误,但如果硬编码这样的值:

.Url(Url.Action("Export", "MyViewModel", new { page = 1, pageSize = "", filter = "", sort = "" }))

它可以正常工作,但控制器只获取页面,其他值将为空。

我也尝试将“grid_Page,...”添加到所有参数但没有任何效果

4

1 回答 1

0

好吧,我的错,我修好了!

由于我使用服务器端填充网格,因此无法调用 js 事件部分:

function onDataBound(e) {
    var grid = $('#List').data('kendoGrid');

在我改成这个之后:

$(document).ready(function () {
 var grid = $('#List').data('kendoGrid');

并删除了这一行:

.Events(ev => ev.DataBound("onDataBound"))

现在它工作正常。

于 2013-09-15T08:00:22.043 回答