kendo grid export给出输入字符串格式错误

时间:2013-09-09 16:47:31

标签: kendo-grid kendo-asp.net-mvc

当我试图将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 = "" }))

它没有错误,但控制器只获取页面,其他值将为null。

我也尝试将“grid_Page,...”添加到所有参数但没有工作

1 个答案:

答案 0 :(得分:0)

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

因为我使用服务器端来填充网格,所以不能调用js事件部分:

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

之后我改为:

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

并删除了这一行:

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

现在它运作正常。