使用Json Request时无法刷新(Response.Flush)

时间:2018-08-28 08:54:19

标签: jquery json ajax asp.net-mvc c#-4.0

我尝试通过ajax请求将数据库中的记录列表上传到Excel,此后刷新,但是不刷新(使用ajax请求时),使用页面加载事件进行刷新,请帮助我这个

这是我的cs代码

 public JsonResult ExportToExcel(string chooseRecords, string toDate, string customerCode, string salesManCode, string salesTeamID)
    {
        Guid companyId = GetCompanyId();
        GridView gridView = new GridView();
        var getAgewise = accountReceivableBL.AgeWiseReportForExportToExcel(companyId, chooseRecords, toDate, customerCode, salesManCode, salesTeamID);

        gridView.DataSource = getAgewise.AccountsReceivables;
        gridView.DataBind();

        using (StringWriter sw = new StringWriter())
        {
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            //To Export all pages
            gridView.AllowPaging = false;
            gridView.HeaderRow.BackColor = Color.Red;
            gridView.AlternatingRowStyle.BackColor = Color.YellowGreen;

            Response.Clear();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment;filename=AgeWiseReport.xls");
            Response.Charset = "";
            Response.ContentType = "application/vnd.ms-excel";
            gridView.RenderControl(hw);
            //style to format numbers to string
            string style = @"<style> .textmode { } </style>";
            Response.Write(style);
            Response.Output.Write(sw.ToString());
            Response.Flush();
            Response.End();
        }
        return Json(true, JsonRequestBehavior.AllowGet);
    }

这是我的Ajax代码

$("#btnExport").click(function () {
    var getRecordTypes = $("#ddlChoseRecordType").val();
    var convertArrayToStrng = "";
    if (getRecordTypes.length > 0) {
        for (var j = 0; j < getRecordTypes.length; j++)
            convertArrayToStrng += getRecordTypes[j] + ",";
    }
    var toDate = $("#AgeWisetxtDateTo").val();
    var chooseRecords = convertArrayToStrng;
    var customerCode = $("#txtAgWiseCustomerCode").val();
    var salesManCode = $("#ddlSalesMan").find('option:selected').val();
    var salesTeamID = $("#ddlSalesTeam").find('option:selected').val();

    $.ajax({
        cache: false,
        url: "/AccountsReceivable/ExportToExcel",
        type: 'POST',
        data: JSON.stringify({ "chooseRecords": chooseRecords, "toDate": toDate, "customerCode": customerCode, "salesManCode": salesManCode, "salesTeamID": salesTeamID }),
        dataType: "json",
        contentType: 'application/json;charset=utf-8',
        beforeSend: function () { $("#progressBarForAgeWise").css("visibility","visible"); },
        success: function (obj1) {
            toastr.success("Exported to excel successfully");
        },

        complete: function () { $("#progressBarForAgeWise").css("visibility", "hidden"); }
    })

})

0 个答案:

没有答案