嗨,我在我的Dotnet核心应用程序中使用第一次视图组件来获取API结果数据。我已经创建了一个类和调用等待API调用,但视图渲染早期,而不是等待API响应。
namespace Gut.Web.Components
{
public class OneWayResultViewComponent : ViewComponent
{
#region Fields
private readonly IApiAggregatorService _apiAggregatorService;
private readonly IWorkContext _workContext;
private readonly ITravelApiProviderService _travelApiProviderService;
#endregion
#region Ctor
public OneWayResultViewComponent(IApiAggregatorService apiAggregatorService, IWorkContext workContext, ITravelApiProviderService travelApiProviderService)
{
_apiAggregatorService = apiAggregatorService;
_workContext = workContext;
_travelApiProviderService = travelApiProviderService;
}
#endregion
#region Methods
public async Task<IViewComponentResult> InvokeAsync(string sessionid)
{
var searchFlightDetails = await _apiAggregatorService.GetCacheSearchFlightDetailsByIdAsync(sessionid);
var travelers = _travelApiProviderService.GetActiveProvider();
var response = _apiAggregatorService.SearchOneWay(travelers, searchFlightDetails).Result;
return View(response);
}
public override
#endregion
}
}
所以我需要显示视图的响应。我在谷歌上搜索,但没有找到一种方法来更新组件。用jQuery调用组件的例子。如果需要使用jQuery我可以调用partialview
我认为视图组件返回了"early"因为你的最后一通电话应该包含await
:
var response = await _apiAggregatorService.SearchOneWay(travelers, searchFlightDetails);