如果异步呼叫错误,请停留在页面上

时间:2018-06-25 19:17:01

标签: asp.net-mvc model-view-controller umbraco

我有一个调用API的异步方法,如果返回的响应是PDF,它将返回文件

如果响应不是pdf(即带有错误值的JSON对象),我如何停留在当前页面上并仅显示错误消息?

该应用程序由Umbraco支持,它当前设置了ViewBag,然后返回RedirectToCurrentUmbracoPage,这意味着该页面仅显示JSON对象

public async Task<ActionResult> QuotationPdfAsync(DbCalculationInput calculation)
        {
            var content = await _apiClient.QuotationAsync(calculation);
            if (content.HasPdf)
            {
                var fileName = $"{calculation.CalculationName}_{DateTime.Now:yyyyMMdd}_{DateTime.Now:HHmmss}.pdf";
                return this.File(content.Pdf, "application/pdf", fileName);
            }

            this.ViewBag.FormError = content.ErrorResponse.FailureDescription;
            return this.RedirectToCurrentUmbracoPage();
        }

1 个答案:

答案 0 :(得分:-1)

    public async Task<ActionResult> QuotationPdfAsync(DbCalculationInput calculation)
    {
        var content = await _apiClient.QuotationAsync(calculation);
        if (content.HasPdf)
        {
            var fileName = $"{calculation.CalculationName}_{DateTime.Now:yyyyMMdd}_{DateTime.Now:HHmmss}.pdf";
            return this.File(content.Pdf, "application/pdf", fileName);
        }
        else
        {
            this.ViewBag.FormError = content.ErrorResponse.FailureDescription;
            return Json("So and so error..");  
        } 
        return this.RedirectToCurrentUmbracoPage();
    }