Converting IPublishedContent to strongly typed viewmodels w/o ModelsBuilder
I have a custom view model that I use in my views. Here is the base class:
public abstract class PageViewModel : RenderModel
{
public PageViewModel(IPublishedContent content) : base(content) { }
public string Title => Content.GetVortoValue<string>("title");
public string MenuName => Content.GetVortoValue<string>("menuName");
public bool ShowInMenu => Content.GetPropertyValue<bool>("showInMenu");
}
So far so good. Now I want to fetch those pages in my backend:
var publishedContent = UmbracoContext.Current.ContentCache.GetAtRoot();
Now I have a collection of IPublishedContent. Is there any way to make Umbraco return the data typed as my different PageViewModel instances instead? Can you associate the published content with its corresponding view model type somehow? If so, I could construct them manually.
As you can see, I'm not using ModelsBuilder, which is because I want to use GetVortoValue values instead of the regular GetPropertyValue.
Currently I'm using my own factory class. Is there any way to make Umbraco use my custom factory?
What you need to do is create a new class file in the same folder where your models are generated. And than add your own implementation of the property. That way models builder will not generate it.
public partial class MyContentType
{
[ImplementPropertyType("myDifferentAlias")]
public string MyProperty { get { return this.GetVortoValue<string>("myDifferentAlias"); } }
}
Thanks for the tip, though I don't think it will help me very much in my case. The reason is that I will rely more or less exclusively on Vorto properties, so I would have to manually override the properties like in your example. So as far as I can tell I might as well rely on my current classes. Please correct me if I'm wrong here.
Is there any way to make Umbraco use a custom factory, so I can have control over the creation of classes (similar to what ModelsBuilder does)?
Converting IPublishedContent to strongly typed viewmodels w/o ModelsBuilder
I have a custom view model that I use in my views. Here is the base class:
So far so good. Now I want to fetch those pages in my backend:
Now I have a collection of
IPublishedContent
. Is there any way to make Umbraco return the data typed as my differentPageViewModel
instances instead? Can you associate the published content with its corresponding view model type somehow? If so, I could construct them manually.As you can see, I'm not using ModelsBuilder, which is because I want to use
GetVortoValue
values instead of the regularGetPropertyValue
.Currently I'm using my own factory class. Is there any way to make Umbraco use my custom factory?
Hi Filur,
You can use GetVortoValue with models builder.
What you need to do is create a new class file in the same folder where your models are generated. And than add your own implementation of the property. That way models builder will not generate it.
Also see the docs : https://github.com/zpqrtbnk/Zbu.ModelsBuilder/wiki/ModelsGenerationConfiguration#implement-property-type
Dave
Hey Dave,
Thanks for the tip, though I don't think it will help me very much in my case. The reason is that I will rely more or less exclusively on Vorto properties, so I would have to manually override the properties like in your example. So as far as I can tell I might as well rely on my current classes. Please correct me if I'm wrong here.
Is there any way to make Umbraco use a custom factory, so I can have control over the creation of classes (similar to what ModelsBuilder does)?
Hi Filur,
Should be possible, but never tried it.
Have a look at this : https://github.com/zpqrtbnk/Zbu.ModelsBuilder/wiki/IPublishedContentModelFactory
Would love to hear if you can get it to work.
Dave
Hi again,
This is what I'm looking for, thanks! I will give it a shot and update you if I manage to get it working as I want to :)
Hi agian Dave. Here is my current implementation which seems to work fine:
My factory:
Registration:
is working on a reply...