4
votes

I am building a site with Sitecore 7.5 and Glass Mapper 3.2.3. When I add a sublayout to the presentation details of a page, Glass Mapper will assume that the data source for the sublayout should be the current page. I can also explicitly set the data source for the rendering by selecting a different page on the site in the presentation details dialog box.

enter image description here

Glass Mapper still works fine and uses the other selected page as the data source for the rendering. However if I attempt to use the Build Query dialog box and build a query for my data source, Glass Mapper seems to ignore that and just use the current page as the data source. Is there some way to use a query that I have built using the Build Query dialog?

enter image description here

EDIT: Here is some more information about my setup. I have a single data template in Sitecore called ContentPage. It has one field: Content. I have multiple pages in the site that use this data template. I have a sublayout called sublayoutPageContent. It inherits from GlassUserControl. All it does is display the Content field. If I add this sublayout to the presentation details for Page A and don't specify a data source it works fine. When I bring up Page A, it displays the Content field from Page A perfectly. I can also go in to the presentation details for the sublayout and tell it to use a different page as the data source - say Page B. If I do that and then I go view Page A, I will see the Content field from Page B. Great - that is working as expected. Now instead of selecting a page for the data source, I use a Query and in that Query I select Page B. Now when I view Page A, I see the Content field for Page A - NOT Page B. Hope that explains it better.

2
Can you please post the part of the code as well?Aman B
There's not really much code to post, Glass handles a lot of this for you. But I can add some basic code I suppose.Corey Burnett
One thing to try is using IEnumerable as your model for glassAman B
I'm not sure I understand what you mean. The models for my data templates are auto-generated by TDS using code generation templates from Glass. The sublayout is a simple sublayout that displays a single text field.Corey Burnett

2 Answers

5
votes

If you're supporting the Page Editor and these sources are going to vary regularly, you might want to use a Datsource Template and Datasource Location instead of using the Datasource. Then, when you add the item via the Page Editor you'll be prompted to create or choose a datasource item. This makes it really easy, especially in the scenario you're describing. You can use static or relative paths for the datasource location. I often use a similar construct, although with more than 1 field, and it works very well. You can use this even if you are not using the Page Editor, but best practices are to support the Page Editor and DMS and this allows you the flexibility of both.

If you need multiple items, this will not work, but based on your description it seems you just need to be able to choose between them. Also, in scenarios like this you can group a number of items under a folder or other item and use the [SitecoreChildren] attribute and just get the items that way. It really all depends on how flexible you need to be and what your requirements are.

0
votes

Because a query can return multiple items you should change your code to expect multiple items instead of single item. Not sure how it works in webforms but in Razor you do something like this:

Change

@inherits Glass.Mapper.Sc.Web.Mvc.GlassView<MyEntity>

To

@inherits Glass.Mapper.Sc.Web.Mvc.GlassView<IEnumerable<MyEntity>>