Search code examples
c#sitecoresitecore8glass-mappersitecore8.1

Sitecore/GlassMapper get the page


I'm working with Sitecore 8.1 and GlassMapper to map the fields in C#. I'm using datasources for the developed components.

I need to be able to take a field from the page we are currently in. Do you know how can I get the page object defined in Sitecore?


Solution

  • For example if you have a Home page and field Title you cast your current page to HomePage model

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

    Where Model is defined like: public HomePage Model { get; set; }

    And in your view you will have:

    @inherits System.Web.Mvc.WebViewPage<Namespace.HomePage>
    
     <title>@Model.Title</title>
    

    Please check also this link: http://glass.lu/Mapper/Sc/Tutorials/Tutorial12

    If you want to make it editable in Page Editor you can use:

    http://glass.lu/Mapper/Sc/Tutorials/Tutorial14