2

I've been trying to achieve the following with glass mapper but can't get it to work.

I have a Home Page template which doesn't have any fields itself but inherits the following two templates:

Navigation Template

Fields: Navigation Title

Meta Information Template

Fields: Page Title, Meta Description

I've created the corresponding interfaces / classes as follows:

[SitecoreType(TemplateId = "{5BAB563C-12AD-4398-8C4A-BF623F7DBCDC}", AutoMap = true)]
public interface INavigation
{
    [SitecoreField(FieldName = "Navigation Title")]
    string NavigationTitle { get; set; }
}

[SitecoreType(TemplateId = "{95539498-31A5-4CB5-8DD6-C422D505C482}", AutoMap = true)]
public interface IMetaInformation
{
    [SitecoreField]
    string PageTitle { get; set; }

    [SitecoreField]
    string MetaDescription { get; set; }
}

[SitecoreType(TemplateId = "{F08693E5-8660-4B13-BBD6-7B9DC6091750}", AutoMap = true)]
public class HomePage : INavigation, IMetaInformation
{
    public virtual string NavigationTitle { get; set; }

    public virtual string PageTitle { get; set; }

    public virtual string MetaDescription { get; set; }
}

When I then try accessing my page all attributes are always null:

var context = new SitecoreContext();
var page = context.GetCurrentItem<HomePage>();

I've tried several different approaches to this but nothing works. Also what was described in different tutorials didn't work. The only thing that works is when I add the fields directly on the Home Page template, but I don't want that since I have more than one page type and I therefore want to inherit the fields.

Does anyone have any idea what I'm missing here?! I'm using Sitecore 7 with .NET 4.5 by the way if that makes a difference.

Marek Musielak
  • 26,832
  • 8
  • 72
  • 80
5earch
  • 289
  • 2
  • 4
  • 15
  • I'm not sure if you can AutoMap on interface level. Try defining explicit [SitecoreField()] attributes on the interface properties and remove the AutoMap property on the interfaces. See if that works. I also suggest you use FieldId="" to map to GUIDs instead of field names. – Ruud van Falier Oct 24 '13 at 14:35
  • Hi Ruud, Thanks for your reply. I removed the automap and added the fieldid instead. Nothing has changed though. Do I need to enable anything (configs or anything)? I feel like it can't be to hard to get this to work, seems like a pretty basic scenario but I've no idea what I'm missing here. – 5earch Oct 24 '13 at 14:59
  • 1
    Have you correctly setup the initialization of Glass so that it load all the models in memory at startup? – Ruud van Falier Oct 24 '13 at 15:31
  • I will try to replicate your issue and get back to you. – Michael Edwards Oct 24 '13 at 16:41
  • 1
    I have tried to replicate your problem but the models worked on my solution. Are your models in a different assembly to the web project? If so read this page http://glass.lu/docs/tutorial/sitecore/tutorial20/tutorial20.html – Michael Edwards Oct 24 '13 at 17:41
  • @Mike I also ran into an issue with tutorial #2. The screen grab shows a field named "Main Body", which won't map to a C# property named MainBody, as the tutorial implies. – Dan Solovay Oct 25 '13 at 01:13
  • @MichaelEdwards Genius! Thanks for reproducing the error with me. I indeed have my models in a different assembly. Your solution fixed the problem. It really threw me off though that it worked when I had declared the field on the template directly and not by inheritance. I would have expected it to work all or nothing :) Thanks everyone for your help, really appreciate it! – 5earch Oct 25 '13 at 06:15
  • @MichaelEdwards Is there also something I have to consider when using the PageEditor? The value gets rendered when I'm not in the PageEditor. When I'm in the PageEditor I don't get the edit frame and no value, just a blank field. Any ideas what could be missing? I'm using @Editable(x => x.NavigationTitle) – 5earch Oct 25 '13 at 07:31
  • @user2668680 were you able to resolve this issue? I do not see an accepted answer or a resolution posted in the thread. – Umeshwali May 30 '14 at 15:37

4 Answers4

2

Your fields are not mapped because you use a space in the Fieldname in the Sitecore Template. Either remove the space or add the attribute [SitecoreField(FieldName ="Page Title")] to the Model.

I think that the Homepage class is trying to map the NavigationTitle on the Homepage template with the fieldName NavigationTitle and ignores the FieldName attribute on the base model.

By the way: I am using only interfaces for the current project I'm working on and it works as expected with inheritance. No need to add a property more then once ;)

Martijn van der Put
  • 4,062
  • 1
  • 18
  • 26
1

Try to set infer type to true. I cannot get it to work at all without having that set.

ex.

item.GlassCast<HomePage>(false, true);

or

context.GetCurrentItem<HomePage>(false, true);

I find it does not work without this set.

Liam
  • 27,717
  • 28
  • 128
  • 190
Davan Etelamaki
  • 181
  • 1
  • 4
0

You should render the common fields in a separate sublayout as a GlassUserControl.

public partial class NavigationTemplate : GlassUserControl<NavigationTemplate>
{

    protected void Page_Load(object sender, EventArgs e)

Here you will have direct access to the NavigationTemplate fields no matter what item you are loading, it will always be cast to NavigationTemplate and will read the field values of the item you are loading.

Nathan Tuggy
  • 2,237
  • 27
  • 30
  • 38
0

It seems that you expect to get the properties on the HomePage instance, but you need to ask for the exact interface that contains the property as seen here

I.e. Instead of doing:

var page = context.GetCurrentItem<HomePage>();

You should explicitly get current item as INavigation and get the field from the interface:

var navigationTitle = context.GetCurrentItem<INavigation>().NavigationTitle;
VilladsR
  • 350
  • 2
  • 10