telerik mvc网格中的excel导出问题

时间:2016-10-08 14:10:42

标签: asp.net-mvc excel export kendo-asp.net-mvc

我有一个telerik mvc网格,其中excel导出功能失败,因为网格的读取操作有额外的参数。 读取操作如下所示:

public ActionResult My_ReadAction([DataSourceRequest]DataSourceRequest request, string startDate, string endDate)
    {
        DateTime _startDate, _endDate;
        _startDate = DateTime.ParseExact(startDate, "yyyyMMdd", CultureInfo.InvariantCulture);
        _endDate = DateTime.ParseExact(endDate, "yyyyMMdd", CultureInfo.InvariantCulture);        
        IQueryable<CM_DISTRIBUTION_SCHEDULE> cm_distribution_schedule = db.CM_DISTRIBUTION_SCHEDULE
            .Where(x => x.REF_DATE >= _startDate && x.REF_DATE <= _endDate);  
        DataSourceResult result = cm_distribution_schedule.ToDataSourceResult(request, cM_DISTRIBUTION_SCHEDULE => new
        {
            RECORD_ID = cM_DISTRIBUTION_SCHEDULE.RECORD_ID,
            REF_DATE = cM_DISTRIBUTION_SCHEDULE.REF_DATE,
            BRANCH_ID = cM_DISTRIBUTION_SCHEDULE.BRANCH_ID,
            CURRENCY = cM_DISTRIBUTION_SCHEDULE.CURRENCY,
            FORECAST_AMOUNT = cM_DISTRIBUTION_SCHEDULE.FORECAST_AMOUNT
        });

        return Json(result);
    }

和网格:

@(Html.Kendo().Grid<MyClass.MyViewModel>().Name("BranchFcast").AutoBind(false)         .HtmlAttributes(new { style = "height: 100%; border: 0;" })
  .Columns(columns =>
  {
    columns.Bound(c => c.REF_DATE).Title("Forecast Date").Format("{0:d}").Width(130);
    columns.ForeignKey(f => f.BRANCH_ID, (System.Collections.IEnumerable)ViewData["branches"], "BranchId", "BranchName").Title("Branch").Width(200);        
    columns.ForeignKey(f => f.CURRENCY, (System.Collections.IEnumerable)ViewData["currencies"], "CurrencyId", "CurrencyName").Title("Currency").Width(150);
    columns.Bound(c => c.FORECAST_AMOUNT).Title("Forecast Amount").Format("{0:c}").Width(150);
    columns.Bound(c => c.TXN_AMOUNT).Title("Amended Forecast").Width(150);        
  })
  .ToolBar(toolbar => {            
        toolbar.Excel();
  }).Excel(excel => excel.FileName("DistSchedule.xlsx").Filterable(true).ProxyURL(Url.Action("Excel_Export_Save", "DistSched")).AllPages(true)) 
  .DataSource(dataSource => dataSource
      .Ajax()
          .PageSize(20)
      .Model(model => 
          {
              model.Id(p => p.RECORD_ID);
              model.Field(p => p.RECORD_ID).Editable(false);
              model.Field(p=> p.REF_DATE).DefaultValue(DateTime.Today);
          }
          )
      .Sort(s => s.Add(f => f.REF_DATE).Descending()).Read(read => read.Action("My_ReadAction", "Controller")))
  )

因此,当单击导出按钮时,它会触发My_ReadAction但不会传递参数,从而导致控制器操作失败。 有没有办法修改导出事件,以便我可以传递参数。欢迎任何其他解决方法。

1 个答案:

答案 0 :(得分:1)

向Ajax DataSource设置添加.Data()流畅方法并实现一个JavaScript函数,该函数将返回具有两个日期的普通对象。这是将自定义参数传递给Grid的Read操作方法的推荐方法。

http://docs.telerik.com/kendo-ui/aspnet-mvc/helpers/grid/binding/ajax-binding#pass-additional-data-to-action-methods