【问题标题】:Update UI using partial view in response of Task Run (Async)使用部分视图更新 UI 以响应任务运行(异步)
【发布时间】:2016-09-12 19:04:24
【问题描述】:

我正在开发 MVC C# Razor Framework 4.6。

我有静态方法 ExportManager.ExportExcelCannedReportPPR 包裹在 Task.Run 中以获取长期运行报告。此方法返回boolean 值并基于此我正在刷新部分视图(_NotificationPanel)。

public ActionResult ExportCannedReport(string cannedReportKey, string cannedReportName)
{            
    string memberKeys = _curUser.SecurityInfo.AccessibleFacilities_MemberKeys; //ToDo: Make sure this is fine or need to pass just self member?
    string memberIds = _curUser.SecurityInfo.AccessibleFacilities_MemberIDs; //ToDo: Make sure this is fine or need to pass just self member?
    string curMemberNameFormatted = _curUser.FacilityInfo.FacilityName.Replace(" ", string.Empty);
    string cannedReportNameFormatted = cannedReportName.Replace(" ", string.Empty);
    string fileName = string.Concat(cannedReportNameFormatted, "_", DateTime.Now.ToString("yyyyMMdd"), "_", curMemberNameFormatted);
    //ToDo: Make sure below getting userId is correct
    string userId = ((_curUser.IsECRIStaff.HasValue && _curUser.IsECRIStaff.Value) ? _curUser.MembersiteUsername : _curUser.PGUserName);

    var returnTask = Task.Run<bool>(() => ExportManager.ExportExcelCannedReportPPR(cannedReportKey, cannedReportName, fileName, memberIds, userId));
    returnTask.ContinueWith((antecedent) =>
    {
        if (antecedent.Result == true)
        {
            return PartialView("_NotificationPanel", "New file(s) added in 'Download Manager'.");
        }
        else
        {
            return PartialView("_NotificationPanel", "An error occurred while generating the report.");
        }
    }, TaskContinuationOptions.OnlyOnRanToCompletion);

    return PartialView("_NotificationPanel", "");
}

现在的问题是,即使 ContinueWith 中的 _NotificationPanel 被执行,UI 也无法刷新。

【问题讨论】:

  • 您真正期望该代码会发生什么?

标签: c# asp.net-mvc razor async-await task


【解决方案1】:

问题是,一旦您从它返回 - 该请求就完成了。您不能为单个请求多次返回。请求和响应是一对一的。您需要在此处使用async 和await,以便在导出完成后才返回结果。

public async Task<ActionResult> ExportCannedReport(string cannedReportKey, 
                                                   string cannedReportName)
{            
    // Omitted for brevity...

    var result = 
        await Task.Run<bool>(() => 
            ExportManager.ExportExcelCannedReportPPR(cannedReportKey, 
                                                     cannedReportName, 
                                                     fileName, 
                                                     memberIds, 
                                                     userId));

    return PartialView("_NotificationPanel", 
        result 
            ? "New file(s) added in 'Download Manager'."
            : "An error occurred while generating the report.");
}

您需要使Task 方法返回,使其“可等待”。然后将该方法标记为async,这将启用await 关键字。最后,您已准备好执行长时间运行的任务,并根据结果正确确定并返回所需的局部视图更新。

更新

或者,您可以在客户端上使用 AJAX 调用,并在服务器响应后更新。有关具体结帐的详细信息MSDN。

【讨论】:

  • 谢谢大卫。实际上,这使用户可以访问其他功能。一旦 Task.Run 执行完成,用户就可以访问其他功能,所以最后它将是同步方法。你能提供你的建议吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多