在自定义命令单击后将kendo网格数据发布到MVC控制器

时间:2017-11-22 15:25:07

标签: excel asp.net-mvc-4 kendo-ui kendo-grid kendo-asp.net-mvc

我试图在点击按钮时发布kendo网格数据。我想将数据保存到excel文件,当数据集很大时,kendo提供的用于保存网格数据的默认选项失败。所以,我试图在服务器端进行excel操作。我的问题是我无法将数据传递给控制器​​。

代码: 查看

def relative_path(*x, __file__):
    this_file_dir = os.path.dirname(os.path.realpath(__file__))
    return os.path.join(this_file_dir, *x)

我尝试在自定义工具栏中使用Url(" Action"," Controller"),它会点击操作,但不会传递数据。当我用HttpPost修饰动作时,它给了我404错误。

使用js函数调用时

@(Html.Kendo().Grid(Model)
.Name("OutputCashGrid")
.Events(ev => ev.Edit("onEdit"))
.Columns(columns =>
{


    columns.Bound(p => p.ClientID).Hidden();

    columns.Bound(p => p.LoadID).Title("Loadid").Hidden();
    columns.Bound(p => p.Level1).Title("Level1").Width(130);
    columns.Bound(p => p.Level2).Title("Level2").Width(130);
    columns.Bound(p => p.AsOfDate).Title("AsOfDate").Width(150).Format("{0:d}").Width(130);

})

  .ToolBar(toolbar =>
              {                  
                  toolbar.Save().HtmlAttributes(new { id = "Save" }).SaveText("Save External Balances and Comments");
                  toolbar.Custom()
                   .Text("Export To Excel")
                   .HtmlAttributes(new { @class = "export" })
                   .Url("javascript:myFunction()");
              })
              .Editable(editable => editable.Mode(GridEditMode.InCell)) // Use in-cell editing mode.
.HtmlAttributes(new { style = "height: 550px;" })

 .Groupable()
          //.Excel(excel => excel
          //    .AllPages(true)

          //                          .FileName("CashSummary_" + @ViewBag.goClientName + "_" + @ViewBag.Asofdate.ToString("MM/dd/yyyy") + ".xlsx")
          //  .Filterable(true)
          //          .ProxyURL(Url.Action("Excel_Export_Save", "SaveRec"))
          //      ) //this fails for large datasets
 .Reorderable(r => r.Columns(true))
 .Sortable()
 .ColumnMenu()
 .DataSource(dataSource => dataSource
    .Ajax()
    .Events(e2 => e2.Change("vChange"))
    .PageSize(50)
    .ServerOperation(false)
         .Batch(true) // Enable batch updates.
                   .Model(model =>
                        {
                            model.Id(p => p.OutputcashID); // Specify the property which is the unique identifier of the model.
                            //model.Field(p => p.OutputcashID).Editable(false); // Make the ProductID property not editable.

     .Update("Editing_Update", "SaveRec",new { loadid = @loadid,clientid=@clientid })
       )


        .Pageable(pageable => pageable
             .Refresh(true)
            .Input(true)
            .Numeric(false)
         )
                     .Resizable(resize => resize.Columns(true))
                     .Selectable()




                )

控制器:

  function myFunction() {
        var grid = $("#OutputCashGrid").getKendoGrid();
        var data = JSON.stringify(grid.dataSource.view());
        $.ajax({
            url: '/SaveRec/Excel_save',
            type: 'POST',
            data: data,
            async: true,
            processData: false
        });
    }

1 个答案:

答案 0 :(得分:0)

要执行整个操作服务器端,您不能通过ajax调用将数据传递给控制器​​。控制器接收您传递的URL,并获取控制器中导出的数据。在它被拉到新服务器端之后,您将传递到工作表生成器。最棘手的部分是在控制器操作抓取之前传入要应用于数据的任何过滤器/页面。在服务器端处理发生时存在加载图像也是有益的。