Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

In ASP.NET Core 6/7, the @Html.RenderAction() method has been replaced by the View Component pattern. View components are more powerful and flexible than RenderAction and enable better separation of concerns.

To use View Components:

  1. Add a new class to your project that inherits from ViewComponent.
  2. Implement a method in your class that returns a PartialView.
  3. In your view, use the <vc> tag helper to invoke the View Component.

For example, here's a simple View Component that returns a PartialView with a list of items:

public class ItemListViewComponent : ViewComponent
{
    private readonly IItemRepository _itemRepository;

    public ItemListViewComponent(IItemRepository itemRepository)
    {
        _itemRepository = itemRepository;
    }

    public async Task<IViewComponentResult> InvokeAsync()
    {
        var items = await _itemRepository.GetAllItemsAsync();
        return View(items);
    }
}

In your view, you can invoke this View Component using the <vc> tag helper:

<vc:item-list></vc:item-list>

Where "item-list" is the name of the View Component. Note that the View Component name is case-insensitive.

This will render the PartialView returned by the View Component within the current view.

You can learn more about View Components in the official ASP.NET Core documentation: https://docs.microsoft.com/en-us/aspnet/core/mvc/views/view-components?view=aspnetcore-6.0.