[SOLVED] Accessing model data in _Layout?
# help-with-umbraco
m
Every content node has a set of SEO properties I hve setup, like meta name, description, tags ,etc. I ahve found my self on each templet having to code the calls for these property and passing them into ViewData so the _Layouout page can read them. After having to add another new property and updating a bunch of templates it accurs to me that has to be an easier way. Is there some way of doing something like
Copy code
Model.Value("propertyName")
in the Layout page so I can just add these properties once and not have to do it in each template?
d
Hi there! As long as all models in all your views implement
IPublishedContent
, you can start your layout with this line:
Copy code
@inherits UmbracoViewPage
That will allow you to do exactly what you want. If you don't always use a model that implements
IPublishedContent
, you can also take
@Umbraco.AssignedContentItem
or
@UmbracoContext.PublishedRequest.PublishedContent
to find the current page. As long as you inject the umbraco helper or umbraco context with the
@inject
keyword
m
So the layout page has
Copy code
@using Umbraco.Cms.Web.Common.PublishedModels;
@inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage
All the child templates are normal Umb13 templates so they do use
Copy code
IPUblsihedContent
I just don't know how to access the properties sionce there is no
Copy code
Model
in the layout template
Ok, I feel stupid. Had to add
Copy code
@using Umbraco.Cms.Core.Models
to the layout page and that gave me access. Thanks
k
If you want strongly typed prop access you can "cast the current page" to the composition which has the SEO props.
7 Views