I have a 2-level menu item: I have a list of department and each department has a list of stores.
I have a Menu, PartialView which iterates through the Model
(departments) and builds the menu:
@model IEnumerable<Department>
<ul>
@foreach (var department in Model)
{
<li>
<a href="#">@Model.DepartmentName</a>
<ul>
@foreach (var store in department.Stores)
{
<li><a href="some-url">@store.StoreName</a></li>
}
</ul>
</li>
}
</ul>
And this is how I call the Menu PartialView in my _layout.cshtml
:
@Html.Partial("Shared/_Menu", MyApplicationCache.departments)
As you can see I am passing the same model (from the cache) to the PartialView on all the requests.
Does Razor ViewEngine have an internal caching system to recognize that this view has already been built (complied to HTML string) for this model? Or does it re-render (recompile) the PartialView on every single request?
The PartialView
gets re-rendered at every single request, assuming you don't have any OutputCacheAttribute
applied on the Controller
or its action method involved.
If you need output caching, you need to set this up explicitly via OutputCacheAttribute
, see the documentation.
You can easily check this by outputting a DateTime
, eg. via a menu-item as shown here below.
At every request, it will show a new value, proving it got re-rendered.
<li><a href="#">@DateTime.Now</a></li>
Full menu:
@model IEnumerable<Department>
<ul>
@foreach (var department in Model)
{
<li>
<a href="#">@Model.DepartmentName</a>
<ul>
<li><a href="#">@DateTime.Now</a></li>
@foreach (var store in department.Stores)
{
<li><a href="some-url">@store.StoreName</a></li>
}
</ul>
</li>
}
</ul>