question

DharmeshSharma avatar image
0 Votes"
DharmeshSharma asked Bruce-SqlWork answered

view Component rendered early not waiting for await methods result in asp.net core

Hi I am using first time view Component in my Dotnet core application to get API result data to view. I have created a class and calling await API call but view render early and not wait for api response.

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

so I need to show response to view. I have search on google but didn't find a way to reupdate component. Example found to call component by jQuery. If jQuery is required so I can call partialview then why component.

dotnet-csharpdotnet-aspnet-core-mvc
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Bruce-SqlWork avatar image
0 Votes"
Bruce-SqlWork answered

your question is not clear. the async render is run on the request.

@await Component.InvokeAsync("OneWayResultViewComponent",new {sessionid = someValue}):

this will render with the passed value.

note: why is line 24 sync? this is bad for performance. s/b

             var response = await _apiAggregatorService.SearchOneWay(travelers, searchFlightDetails);


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.