1

Inside Tridion 2013-SP1 with DD4T template 2.0.8

All our schema's Root element name are the same (e.g. schema Content has a root element of "Content" and schema Accordion List has a root element of "Content" too)

So, in our .net MVC code we are implementing viewModel. Below is the code for AccordionList viewModel.

    [ContentModel("Content", true)]
    public class AccordionList : ViewModelBase, IRenderableViewModel
    {
        [TextField(FieldName = "title")]
        public string title { get; set; }

        [TextField(FieldName = "description")]
        public string description { get; set; }

        [RenderData]
        public IRenderData RenderData { get; set; }
     }

Then here is the code for Content ViewModel.

    [ContentModel("Content", true)]
    public class Content : ViewModelBase, IRenderableViewModel
    {
        [TextField(FieldName = "title")]
        public string title { get; set; }

        [TextField(FieldName = "body")]
        public string description { get; set; }

        [RenderData]
        public IRenderData RenderData { get; set; }
     }

When we run the website we get this error message: The model item passed into the dictionary is of type 'DD4T.SampleWebSite.Models.Component.AccordionList', but this dictionary requires a model item of type 'DD4T.SampleWebSite.Models.Component.Content'.

I think because they have the same root element schema and that AccordionList is the first viewModel in the list even if it really need the viewModel Content. The AccordionList is the one that gets register.

So my question now is that is there anyway we can fix the issue without changing tridion schema root element name?

1

You are correct, DD4T resolves the type of the model based on the Schema Root Element name.

The best practice would be to have unique names for the SchemaRootElement but if you don't want to do that and find a workaround your best bet is to implement your own ContentModelAttribute (IsMatch method) and bind IContentModelAttribute to your CustomContentModelAttribute implementation.

Have a look here to see how it's implmented in DD4T: https://github.com/dd4t/DD4T.Core/blob/develop/source/DD4T.ViewModels/AttributesBase.cs

@Siawash, thanks for your inputs ;)

| improve this answer | |
  • Thank you @Kunal and everyone! This is exactly what i needed! i Tried it and it works. Instead of using the root element name we use the schema name. :) – Randy Honorato Jun 20 '16 at 7:27
  • Please mark it as the answer if it helped solve your problem, it helps the community as well ;) – Kunal Jun 20 '16 at 7:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.