Does not contain a definition for Content or typed content error in Version 8
I was using this code in site layout in version 7 and this was working fine but now in version 8 this throws error in razor view:
Can someone guide me about what i am doing wrong?
@inherits Umbraco.Web.Mvc.UmbracoViewPage
@{
Layout = null;
var home = Model.Content.AncestorOrSelf(1);
var homepage = Umbraco.TypedContentAtRoot().First().DescendantsOrSelf().FirstOrDefault(x => x.DocumentTypeAlias == "homePage");
}
Are you just trying to get the homepage? if so the line you're using
var home = Model.Content.AncestorOrSelf(1);
Should work without the need of the second line. What is it you're trying to get?
Looking at the answer and good explanation here it could be that because you're inheriting from a View Page you don't need .Content to access the Model property. So perhaps try var home = Model.AncestorOfSelf(1);
var homepage = Umbraco.TypedContentAtRoot().First().DescendantsOrSelf().FirstOrDefault(x => x.DocumentTypeAlias == "homePage");
is unnecessarily complex.
The First() method can return null, then you will get a null reference exception. Also, when the node is not the first root child anymore, you will get unexpected results.
DescendantsOrSelf() is far to heavy in your situation, because the homepage is always on one of the first levels in the tree. Using DescendantsOrSelf causes Umbraco to look to all the descendants.
In Umbraco 8 the RenderModel has been removed, so it is no longer Model.Content just Model.
Also note that some extensions methods have changed, e.g. .Site() is now called .Root()which basically just wraps Model.Content.AncestorOfSelf(1) (v7) and Model.AncestorOfSelf(1) (v8).
I guess this should be sufficient to grap homepage node in most sites if the homepage node is on root level with other pages as children or descendants.
@inherits Umbraco.Web.Mvc.UmbracoViewPage
@{
Layout = null;
var home = Model.Root();
}
Furthermore I think the property DocumentTypeAlias has been removed and you can access alias from IPublishedContent via .ContentType.Alias
Does not contain a definition for Content or typed content error in Version 8
I was using this code in site layout in version 7 and this was working fine but now in version 8 this throws error in razor view: Can someone guide me about what i am doing wrong?
Hi Rabea,
What error are you getting?
Are you just trying to get the homepage? if so the line you're using
Should work without the need of the second line. What is it you're trying to get?
Looking at the answer and good explanation here it could be that because you're inheriting from a View Page you don't need
.Content
to access the Model property. So perhaps tryvar home = Model.AncestorOfSelf(1);
Getting the homepage by using
is unnecessarily complex.
I would suggest you to use:
Or better, with ModelsBuilder:
Hi Rabea
In Umbraco 8 the
RenderModel
has been removed, so it is no longerModel.Content
justModel
.Also note that some extensions methods have changed, e.g.
.Site()
is now called.Root()
which basically just wrapsModel.Content.AncestorOfSelf(1)
(v7) andModel.AncestorOfSelf(1)
(v8).I guess this should be sufficient to grap homepage node in most sites if the homepage node is on root level with other pages as children or descendants.
Furthermore I think the property
DocumentTypeAlias
has been removed and you can access alias fromIPublishedContent
via.ContentType.Alias
/Bjarne
Thanks a lot Bjarne, makes a lot more sense now
is working on a reply...