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:
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.