Umbraco Is successfully creating the Model classes and I can see them in my visual studio project.
In my controller, I'm using one of the generated models and I am getting all the values. For example:
public ActionResult Index(Home model)
{
//model is now a strongly typed (Home Document type)
var test = model.Descendants("myContentTypeAlias")
}
In the example above the variable test is a collection of IPublishedContent - My question is how can I make that list strongly typed based on the models that Umbraco has created.
I got just a little issue with this, my query returns me 0 result.
Is there anyway to specify a document type as a Custom Model or I am missing something?
public class ProductModel : ContentModel
{
public ProductModel(IPublishedContent content) : base(content)
{
}
}
@{
var allProducts = Model.Descendants("product").OfType<ProductModel>();
foreach (ProductModel product in allProducts)
}
Model Builder - How to covert IPublishedContent in to strongly typed Model
Hi Guys,
I'm using the out of the box Model Builder that comes with Umbraco I have enabled it in the web.config file like so:
Umbraco Is successfully creating the Model classes and I can see them in my visual studio project.
In my controller, I'm using one of the generated models and I am getting all the values. For example:
In the example above the variable test is a collection of IPublishedContent - My question is how can I make that list strongly typed based on the models that Umbraco has created.
Thanks in advance.
Hi Ayo
You can add
.OfType<MyContentAlias>()
tomodel.Descendants("myContentTypeAlias")
to get your collection in the right model.Thank you! - Exactly what I needed.... do you know where this is in the documentation, without your answer I would have struggled to find that.
Thanks!
Thanks Soren!
I got just a little issue with this, my query returns me 0 result.
Is there anyway to specify a document type as a Custom Model or I am missing something?
Hi Marc-Andre
Is your
ProductModel
a model generated by ModelsBuilder? It has to be, else you can't cast it that way.Yes it is!
Now I got the error "Cannot bind ProductModel to ContentModel"
Ok got it. I had to pass my IPublishedContent as an agrument -_- hehe
Hi,
You could probably also try the following:
Jeroen
is working on a reply...