简体   繁体   中英

Async in MVC Controller, multiple await i need

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

Trying to implement Async in my Controller. I have to call ShowUploadFiles() which is to be saved in Tempdata and GetListofCommentsfromTable() to be called and pass it as Model.

How can i impelement Async to both as Tempdata and Model has to be loaded independently.

Do I have to update as

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

         }

You can do it like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM