2016-04-22 81 views
0
public async Task<ActionResult> Print(BasicSurveyReportViewModel paramModel) 
{ 

    ActionResult OverallSummary = 
     await basicSurveyReportController.OverallSummary(paramModel); 
    ActionResult CompanyInfo = 
     await basicSurveyReportController.CompanyInfo(paramModel); 
    ViewBag.OverallSummary = OverallSummary; 
    ViewBag.CompanyInfo = CompanyInfo; 
    return View(); 
} 

在此代碼中,首先成功設置了OverallSummary變量。在異步方法中等待覆蓋以前的變量,多重等待

但是,當運行在CompanyInfo行的方法時,它將該變量放入CompanyInfoOverallSummary

如何將結果分解爲單獨的變量?

+1

您的問題缺乏清晰度。你是說'ViewBag.OverallSummary'和'ViewBag.CompanyInfo'都指向方法結尾的同一個對象嗎? – yaakov

+0

覆蓋最新的await結果的兩個ActionResult變量的頂部。它也會影響'ViewBag.OverallSummary'和'ViewBag.CompanyInfo'。 – jemrlee

+1

這不是一句話。在'CompanyInfo'線的方法運行時,** **,但 你的意思是這條線: – yaakov

回答

1

看起來你正試圖調用異步子動作,這在ASP.NET 4.x中不受支持。我懷疑ASP.NET控制器只是在您返回View(blah)或您的子操作中的任何內容時才訪問當前的請求。

正確地做到這一點(在ASP.NET的當前版本)的唯一方法是寫helper方法做實際的工作,回到自己的類型(不ActionResult):

OverallSummary GetOverallSummaryAsync(BasicSurveyReportViewModel model); 
CompanyInfo GetCompanyInfoAsync(BasicSurveyReportViewModel model); 

public async Task<ActionResult> Print(BasicSurveyReportViewModel paramModel) 
{ 
    OverallSummary overallSummary = 
    await GetOverallSummaryAsync(paramModel); 
    CompanyInfo companyInfo = 
    await GetCompanyInfoAsync(paramModel); 
    ViewBag.OverallSummary = overallSummary; 
    ViewBag.CompanyInfo = companyInfo; 
    return View(); 
} 

然後你其他控制器操作也可以轉換爲使用這些方法,如下所示:

public async Task<ActionResult> OverallSummary(BasicSurveyReportViewModel paramModel) 
{ 
    return View(await GetOverallSummaryAsync(paramModel)); 
}