ASP.NET Core MVC Interview Question #162
How do you invoke a View Component from Razor?
Testing, AJAX/JavaScript Integration, Localization & Advanced MVC Senior Advanced
Quick Interview Answer
A View Component can be invoked with Component.InvokeAsync or through a View Component Tag Helper when its naming and registration conventions are satisfied. The invocation can pass arguments that match the component's Invoke or InvokeAsync parameters.
Detailed Explanation
A View Component can be invoked with Component.InvokeAsync or through a View Component Tag Helper when its naming and registration conventions are satisfied. The invocation can pass arguments that match the component's Invoke or InvokeAsync parameters.
The component returns an IViewComponentResult, usually rendering its own Razor view.
Code Example
@await Component.InvokeAsync(
"CartSummary",
new { showTotal = true })
@* Tag Helper form can also be used when applicable. *@