weixin_33728268 2019-11-20 05:25 采纳率: 0%
浏览 39

如何从ajax渲染视图?

I am using asp .net mvc along with telerik mvc ui and telerik reporting, I have a controller

    [HttpPost]
    public ActionResult GenerateReport(List<Guid> invoiceIds)
    {
        var apvReports = _reportsLogic.GenerateAccountPayableVouchers(invoiceIds);
        string json = JsonConvert.SerializeObject(apvReports);
        string reportSource = typeof(AccountsPayableVoucher).AssemblyQualifiedName + "--" + json;

        return View("CustomReportViewer", (object)reportSource);
    }

it is being called by my ajax in the view, here is my ajax function

<script>
function generateAccountsPayableReports() {
    var ms = $("#msInvoicesAPV").data("kendoMultiSelect");
    var invoices = ms.dataItems();
    var invoiceIds = [];

    invoices.forEach(function (invoice) {
        invoiceIds.push(invoice.Id);
    });

    $.ajax({
        type: "POST",
        url: "/APV/GenerateReport ",
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify({ 'invoiceIds': invoiceIds }),
        success: function (result) {
            if (result) {
                var w = window.open();
                $(w.document.body).html(result);
            }
        },
        failure: function (result) {
            console.error("result: ", result);
        },
        error: function (result) {
            console.error("result: ", result);
        }
    });
}

I am getting a error such as

error

I have tried this and it works, here is the js and the controller

<script>
function generateAccountsPayableReports() {
    var ms = $("#msInvoicesAPV").data("kendoMultiSelect");
    var invoices = ms.dataItems();
    var invoiceIds = [];

    invoices.forEach(function (invoice) {
        invoiceIds.push(invoice.Id);
    });

    $.ajax({
        type: "POST",
        url: "/APV/GenerateReportData ",
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify({ 'invoiceIds': invoiceIds }),
        success: function (result) {
            if (result) {
                var encodedResult = encodeURIComponent(result);
                window.open('/APV/GenerateReport?reportSource=' + encodedResult)
            }
        },
        failure: function (result) {
            console.error("result: ", result);
        },
        error: function (result) {
            console.error("result: ", result);
        }
    });
}

    [HttpPost]
    public string GenerateReportData(List<Guid> invoiceIds)
    {
        var apvReports = _reportsLogic.GenerateAccountPayableVouchers(invoiceIds);
        string json = JsonConvert.SerializeObject(apvReports);
        string reportSource = typeof(AccountsPayableVoucher).AssemblyQualifiedName + "--" + json;

        return reportSource
    }

    public ActionResult GenerateReport(string reportSource)
    {
        return View("CustomReportViewer", (object)reportSource);
    }

but it has a flaw, if the data is too large it is not good to pass it by URL

what is the best approach in this kind of scenario?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 delta降尺度方法,未来数据怎么降尺度
    • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
    • ¥15 高德地图点聚合中Marker的位置无法实时更新
    • ¥15 DIFY API Endpoint 问题。
    • ¥20 sub地址DHCP问题
    • ¥15 delta降尺度计算的一些细节,有偿
    • ¥15 Arduino红外遥控代码有问题
    • ¥15 数值计算离散正交多项式
    • ¥30 数值计算均差系数编程
    • ¥15 redis-full-check比较 两个集群的数据出错