I've been able to successfully query page info from within a partial view, for example:
var info = Umbraco.TypedContent(2016).Children("foo").Where(x => x.IsVisible());
Is it possible to access the same information from within the model .cs file? Ideally, whenever a new instance of the model is created, I'd like to execute some code within the constructor that will populate one of the properties with info from a custom document type.
Or, if I'm thinking about this all wrong, let me know.
Thanks,
Pam
UPDATE: I found the documentation I was looking for and was able to access most of the functionality with UmbracoHelper:
var umbracoHelper = new Umbraco.Web.UmbracoHelper(Umbraco.Web.UmbracoContext.Current);
Just had to make sure I added using System.Linq and Umbraco.Web
The scope and life-cycle of a model is not specified. In other words, you don't know whether the model exists only for you and for the context of the current request, or if it is cached by Umbraco and shared by all requests. Even though as of version 7.4 Umbraco creates distinct models for each request, this will not always be true.
As a consequence, the following code has a major issue: the TextPage model "caches" an instance of the HomePageDocument model that will never be updated if the home page is re-published.
public partial class TextPage
{
public TextPage(IPublishedContent content)
: base(content)
{
HomePage = content.AncestorOrSelf(1) as HomePageDocument;
}
public HomePageDocument HomePage { get; private set; }
}
As a rule of thumb, models should never, ever reference and cache
other models.
Querying document types in model constructor
Hello,
I've been able to successfully query page info from within a partial view, for example:
var info = Umbraco.TypedContent(2016).Children("foo").Where(x => x.IsVisible());
Is it possible to access the same information from within the model .cs file? Ideally, whenever a new instance of the model is created, I'd like to execute some code within the constructor that will populate one of the properties with info from a custom document type.
Or, if I'm thinking about this all wrong, let me know.
Thanks, Pam
UPDATE: I found the documentation I was looking for and was able to access most of the functionality with UmbracoHelper:
var umbracoHelper = new Umbraco.Web.UmbracoHelper(Umbraco.Web.UmbracoContext.Current);
Just had to make sure I added using System.Linq and Umbraco.Web
Hi Pam
This is bad practise, and explained in the docs:
is working on a reply...