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

Dharmesh Sharma 1 Reputation point
2021-10-26T10:01:15.547+00:00

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.

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,080 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,100 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 53,506 Reputation points
    2021-10-26T17:54:57.91+00:00

    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);  
    
    
      
    
    0 comments No comments