MVC控制器中的异步,多个等待我需要

时间:2013-09-13 10:11:10

标签: c# asp.net-mvc asynchronous

 public async Task<ActionResult> IndexAsync()
        {
            ShowUploadFiles objFiles = new Helpers.ShowUploadFiles();
            ShowComments objComments = new Helpers.ShowComments();
            TempData["FileUploaded"] = await objFiles.ShowUploadeFiles();
            return View("Index", await objComments.GetListofCommentsfromTable());
        }

尝试在我的Controller中实现Async。   我必须调用ShowUploadFiles()来保存在Tempdata中,并调用GetListofCommentsfromTable()并将其作为模型传递。

我如何能够作为Tempdata和Model两者的异步同步加载。

我必须更新为

public async Task<List<BlobModel>> ShowUploadeFilesAsync()
        {
           //Some operation
           return await (List<BlobModel>);

         }

1 个答案:

答案 0 :(得分:3)

你可以这样做:

 public async Task<ActionResult> IndexAsync()
 {
     ShowUploadFiles objFiles = new Helpers.ShowUploadFiles();
     var showUploadeFilesTask = objFiles.ShowUploadeFiles();

     ShowComments objComments = new Helpers.ShowComments();
     var getListofCommentsfromTableTask = objComments.GetListofCommentsfromTable();

     await Task.WhenAll(showUploadeFilesTask, getListofCommentsfromTableTask);

     TempData["FileUploaded"] = showUploadeFilesTask.Result;
     return View("Index", getListofCommentsfromTableTask.Result);
 }