Partial Views
This section will show you how to use MVC Partial Views in Externable. Please note, this is documentation relating to the use of native MVC partial views, not 'Partial Macro Views'
Overview
Using Partial Views in Umbraco is exactly the same as using Partial Views in a normal ASP.NET MVC project. There is detailed documentation on the Internet about creating and using MVC partial views
Partial views allow you to re-use components between your views (templates).
Example
A quick example of a content item that has a template that renders out a partial view template for each of its child documents:
The MVC template markup for the document:
@inherits Umbraco.Web.Mvc.UmbracoViewPage
@{
Layout = null;
}
<html>
<body>
@foreach(var page in Model.Children.Where(x => x.IsVisible())){
<div>
@Html.Partial("ChildItem", page)
</div>
}
</body>
</html>
The partial view (located at: ~/Views/Partials/ChildItem.cshtml
)
@model IPublishedContent
<strong>@Model.Name</strong>
Caching
You don't normally need to cache the output of Partial views, but there are times when this is necessary. Like macro caching, we provide caching output of partial views. This is done by using an HtmlHelper extension method:
@Html.CachedPartial("MyPartialName", new MyModel(), 3600)
The above will cache the output of your partial view for one hour (3600 seconds). Additionally, there are a few optional parameters you can specify to this method. Here is the full method signature:
IHtmlString CachedPartial(
string partialViewName,
object model,
int cachedSeconds,
bool cacheByPage = false,
bool cacheByMember = false,
ViewDataDictionary viewData = null,
Func<object, ViewDataDictionary, string> contextualKeyBuilder = null)
So you can specify to cache by member and/or by page and also specify additional view data to your partial view. However, if your view data is dynamic (meaning it could change per page request) the cached output will still be returned. This same principle applies if the model you are passing in is dynamic. Please be aware of this: if you have a different model or viewData for any page request, the result will be the cached result of the first execution. If this is not desired you can generate your own cache key to differentiate cache instances using the contextualKeyBuilder parameter
To create multiple versions based on one or more viewData parameters you can do something like this:
@Html.CachedPartial("MediaGallery", Model, 3600, true, false, new ViewDataDictionary { { "year", Request.QueryString["year"] } }, (model, viewData) => viewData["year"].ToString() + viewData["Parameter2"].ToString() )
Or using a custom helper function:
public static Func<object, ViewDataDictionary, string> CacheBy(this HtmlHelper htmlHelper, params string[] keys)
{
return (model, viewData) => String.Join("", keys.Select(s => viewData[s].ToString()));
}
@Html.CachedPartial("MediaGallery", Model, 3600, true, false, new ViewDataDictionary { { "year", Request.QueryString["year"] } }, Html.CacheBy("year", "Parameter2") )
Or even based on a property on the Model (though if Model is the current page then cacheByPage
should be used instead):
@Html.CachedPartial("MediaGallery", Model, 3600, true, false, new ViewDataDictionary { }, (model, viewData) => model.myField )
Regardless of the complexity here the contextualKeyBuilder function needs to return a single string value.
Cache of all CachedPartials is emptied on Externable content publish events.