0

I have the following set up in my CMS

enter image description here

Under ContactUsPageSettings I have some items that are using a template called ContactUsQuestionsOption and then below that I have some items that are using a template called ContactUsSubQuestionOption.

I want to return all of the ContactUsQuestionsOption in a drop down list, which I have managed to do. The problem I'm having is having a second drop down list that only returns the children of the selected item. Looking at the image above if Faculty Research and Publications was selected I'd want the second drop down to show BSR, Faculty Enquiries and ResearchSupportEnquiries.

I have a view model that looks like this:

    [SitecoreType(AutoMap = true)]
public class ContactUsPageSettingsViewModel : ContactUsPageSettings
{
    [SitecoreQuery(".//*[@@templatekey='contactusquestionsoption']", IsLazy = false, IsRelative = true)]
    public IEnumerable<ContactUsQuestionsOption> QuestionsOptionItems { get; set; }

    [SitecoreQuery("./child::*", IsLazy = true, IsRelative = true)]
    public virtual IEnumerable<ContactUsPageSettingsViewModel> Children { get; set; }

}

And a second view model like this:

[SitecoreType(AutoMap = true)] public class QuestionsViewModel : ContactUsQuestionsOption { [SitecoreQuery(".//*[@@templatekey='contactussubquestionsoption']", IsLazy = false, IsRelative = true)] public virtual IEnumerable QuestionsOptionItems { get; set; }

}

But this doesn't appear to be the answer. Any ideas?

3
  • Can you clarify which items are using the Question Option and which items are using the Sub Question template?
    – DougCouto
    Feb 16, 2017 at 22:25
  • I've updated my question to clarify my issue.
    – gadgetgem
    Feb 17, 2017 at 11:02
  • Looking at GlassMapper I'm thinking I need to expose the parent guid. Is this the right idea?
    – gadgetgem
    Feb 17, 2017 at 11:12

2 Answers 2

0

I finally worked out a solution for this. I changed my parent item in GlassBase adding the declaration [SitecoreParent(InferType = true)] above Parent. I can now filter my subquestions based on the parent id.

Thanks

Gemma :)

1
  • Glad you were able to figure it out. In my answer, you'll see I simplified your code by replacing query SitecoreQuery properties with a property decorated with SitecoreChildren.
    – DougCouto
    Feb 17, 2017 at 13:52
0

Since you have 3 unique data templates in Sitecore, you need 3 unique Glass-based models. Also, you don't have to use the query property in any of the classes, you can simply use the Children property.

Side note, though not necessary to accomplish what you are trying to do, you may find it helpful to ensure all of your Glass models have, at least, the properties in the GlassBase class below.

So heres what I would do:

Create a base class that contains these properties and update all other Glass models to inherit this class and they will have the children property properly mapped (as well as the Parent property).

public class GlassBase
{
    public Guid Id { get; set; }

    [SitecoreParent(InferType = true)]
    public GlassBase Parent { get; set; }

    [SitecoreChildren(InferType = true)]
    public GlassBase Children { get; set; }
}

Now your template-specific models can simply inherit this class:

[SitecoreType(TemplateId = "Id of Data Template", AutoMap = true)]
public class ContactUsPageSettings : GlassBase
{
}

[SitecoreType(TemplateId = "Id of Data Template", AutoMap = true)]
public class ContactUsQuestionsOption : GlassBase
{
}

[SitecoreType(TemplateId = "Id of Data Template", AutoMap = true)]
public class ContactUsSubQuestionOption : GlassBase
{
}

I'm leaving out properties that map to each of the fields in these templates because I don't know what fields you have in each template.

Side note, make sure you set the TemplateId attribute of the SitecoreType decorator at the top of each of your Glass models to ensure they map to the proper data templates in Sitecore. GlassBase does not need the SitecoreType decorator.

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.