I am working on a project using ASP.NET Core 5.0 to build a web app (MVC). I tried to use the Html.RenderAction
extension method but I get the following error.
COMPILE ERROR:
Html.RenderAction CS1061: IHtmlHelper<dynamic> does not contain a definition for 'RenderAction'
What I'm I not doing right?
To give more context, I want to render a partial view in _Layout.cshtml
using data loaded directly from the database.
This is the content of my _Layout.cshtml
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384- EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
<link rel="stylesheet" href="~/dist/css/main.css" media="all" />
<title>@ViewData["Title"]: my webpage</title>
</head>
<body id="body">
<!-- Header -->
<header id="header">
<!-- Navigation -->
<nav id="nav">
<partial name="../Shared/Components/_navBar.cshtml" />
<partial name="../Shared/Components/_headerSearchBar.cshtml" />
</nav>
@{ Html.RenderAction("GetCategories"); }
</header>
<!-- Main -->
<main role="main" class="main-wrapper">
<div class="home main-content">
@RenderBody()
</div>
</main>
<!-- Footer -->
<footer class="footer">
<partial name="../Shared/Components/_footer.cshtml" />
</footer>
<script src="~/js/script.js" asp-append-version="true"></script>
@*<script src="~/js/scriptanim.js" asp-append-version="true"></script>*@
@await RenderSectionAsync("Scripts", required: false)
</body>
</html>
This is the action method in the controller that loads data to the partial view
public async Task<IActionResult> GetCategories()
{
var categories = await _fashionBuyRepository.GetAllCategoriesAsync();
return PartialView("_productCategoriesHeader", categories);
}