versionFrom |
---|
8.8.0 |
Built-in properties, which exists on all content objects by default
Common Examples
@* gets the current page Url *@
@Model.Url
@* gets the Creation date, and formats it to a short date *@
@Model.CreateDate.ToString("D")
@* Outputs the name of the parent if it exists *@
@if(Model.Parent != null){
<h1>@Model.Parent.Name</h1>
}
Returns the unique Id for the current content item
@Model.Id
Returns the Name of the current content item in the current culture
@Model.Name
Returns the Name of the current content item in the specified culture, null falls back to the current culture
@Model.Name()
Returns a strongly typed 'PublishedContentType' object representing the content type the IPublishedContent item is based on, that gives access to the alias
@Model.ContentType
@Model.ContentType.Alias
Returns a culture from a configured domain in the content tree.
@Model.GetCultureFromDomains(Model.Url)
Returns the parent content item
@Model.Parent
@Model.Parent.Name
Returns a comma delimited string of Node Ids that represent the path of content items back to root.
@Model.Path
Returns the Level (depth) this content item is in its tree path
@Model.Level
Returns the id of the default Template object used with this content item.
@Model.TemplateId
There are extension methods to retrieve template alias (Model.GetTemplateAlias())
Returns the index the page is on, compared to its siblings
@Model.SortOrder
Returns the complete Url to the page in the current culture
@Model.Url
Returns the Url to the page.
This can be used to get the Url for a specific culture, and for getting the Url in a specific (mode)[https://our.umbraco.com/apidocs/v8/csharp/api/Umbraco.Core.Models.PublishedContent.UrlMode.html].
@Model.Url()
Example: Getting a Danish Url for a site where a Danish language has been set up.
@Model.Url("dk")
Example: Getting an Absolute Danish Url for a site where a Danish language has been set up.
@Model.Url("dk", UrlMode.Absolute)
Returns the Url encoded name of the page (slug) of the current culture
@Model.UrlSegment
Returns the Url encoded name of the page (slug) of the specified culture
@Model.UrlSegment()
Returns the id of the Umbraco backoffice user that performed the last update operation on the content item.
@Model.WriterId
Returns the name of the Umbraco backoffice user that performed the last update operation on the content item.
deprecated Will be removed in future versions of Umbraco, use WriterName(IUserService) instead
@Model.WriterName
Returns the name of the Umbraco backoffice user that initially created the content item.
@Model.WriterName(IUserService)
Returns the id of the Umbraco backoffice user that initially created the content item
@Model.CreatorId
Returns the name of the Umbraco backoffice user that initially created the content item.
deprecated Will be removed in future versions of Umbraco, use CreatorName(IUserService) instead
@Model.CreatorName
Returns the name of the Umbraco backoffice user that initially created the content item.
@Model.CreatorName(IUserService)
Returns the DateTime the page was created
@Model.CreateDate
@* gets the Creation date, and formats it to a short date *@
@Model.CreateDate.ToString("D")
Returns the DateTime the page was modified
@Model.UpdateDate
@* gets the Update/Modified date, and formats it to a short date *@
@Model.UpdateDate.ToString("D")
All content and media items contain a reference to all the data defined by their document type.
Custom property access is achieved using variations of the method: Value
Returns the property value for the specified property alias
@*Get the property with alias: "siteName" from the current page *@
@Model.Value("siteName")
The type returned of this property value is object
which is fine in most cases since when using
the above syntax, Razor will automatically execute a ToString()
on the result value.
See Model.Value<T>(string)
for how to return a strongly typed object for the property
Returns the property value for the specified property alias converted to 'T' - the requested output type of the property value.
For example, to return the string
result of "siteName":
@(Model.Value<string>("siteName"))
Or to return a collection of IPublishedContent
for a multiple media picker
var mediaItems = Model.Value<IEnumerable<IPublishedContent>>("mediaIds");
Another example might be if a property editor stores a JSON value, it might then support converting to a custom
strongly typed model, or at the very least the JSON would be convertible to a JObject
instance, for example:
@(Model.Value<NestedContentModel>("nestedContent"))
or
@(Model.Value<JObject>("nestedContent"))
If the current content item doesn't have the requested value, use an alternative 'fallback' value in its place.
If a content page has a 'title' property, to fallback to use the 'Name' of the content item if the 'title' is not populated. Set the Fallback type to be Fallback.ToDefaultValue, and set the DefaultValue accordingly:
@Model.Value<string>("title", fallback: Fallback.ToDefaultValue, defaultValue: Model.Name);
or to a specific value
@Model.Value<string>("author", fallback: Fallback.ToDefaultValue, defaultValue: "Team Reporter");
Look for a property value on the current page. If it doesn't exist look for the property value on the parent page. Then the parent's parent page and so on. All the way up the content tree - this approach allows the specification of 'global property values' that can then be overridden in different sections or on individual pages.
@Model.Value("propertyAlias", fallback: Fallback.ToAncestors)
If working with variants - fallback to a different language value - if perhaps the value hasn't been populated yet for the current language:
Model.Value("pageTitle", "fr", fallback: Fallback.ToLanguage)
Use Fallback.To() to 'combine' Fallback options.
The following would first look for a 'title' property on all ancestors, before defaulting to the current page's name:
@Model.Value("title", fallback: Fallback.To(Fallback.Ancestors, Fallback.DefaultValue), defaultValue: Model.Name)
There are a few helpful methods to help check if a property exists, has a value or is null.
Returns a boolean value representing if the IPublishedContent has a property with the specified alias.
Returns a boolean value representing if the IPublishedContent property has had a value set.
It's possible to use 'Fallbacks' with HasValue:
bool hasPageTitleSetSomewhere = Model.HasValue("pageTitle",fallback: Fallback.ToAncestors);