如何从ajax渲染视图?

时间:2019-11-20 05:25:51

标签: javascript c# asp.net ajax telerik

我正在使用asp .net mvc和telerik mvc ui和telerik报告,我有一个控制器

    [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);
    }

我的ajax在视图中正在调用它,这是我的ajax函数

<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);
        }
    });
}

我收到诸如

之类的错误

error

我已经尝试过了,并且可以正常工作,这里是js和控制器

<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);
    }

但它有一个缺陷,如果数据太大,则不能通过URL传递

在这种情况下最好的方法是什么?

0 个答案:

没有答案