2

I have a Article View Rendering that contain Datasource Location that points to its Article Datasource item. The view rendering cshtml file is Article.cshtml. I'm using Glass Mapper to map Article Model on View.

[SitecoreType(TemplateId = "{3C578C91-12FD-4E43-A9EB-4AEB053D0F8E}", AutoMap = true)]
public class Article
{
    [SitecoreId]
    public virtual Guid ID { get; set; }
    [SitecoreField]
    public virtual string Tittle { get; set; }

    [SitecoreField]
    public virtual string Text { get; set; }

    [SitecoreField]
    public virtual string Colour { get; set; }
}

But on Page get Article Rendering,i have Article Metadata section,i want to get field of that on Views include Article.

[SitecoreType(TemplateId = "{B3B677AC-1C3B-44E8-91B7-14B42C77058E}", AutoMap = true)]
public class WebPage : IArticleMetaData
{
    [SitecoreField]
    public virtual string Title { get; set; }
    [SitecoreField]
    public virtual string Description { get; set; }

}

[SitecoreType(TemplateId = "AACF09E7-A7B8-4D32-946B-B84098738BEE", AutoMap = true)]
public interface IArticleMetaData
{
    [SitecoreField]
      string Title { get; set; }

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

Any soloution for that?Thanks

4

So it sounds like you are trying to get access to a datasource (Article) item as well as the current context item (WebPage). To do that, you would need to use a controller rendering. And within that controller rendering, you would want to have code that looks something like this:

public ActionResult Display()
{
   var model = new ArticleViewModel();

   model.Article = GetDataSourceItem<Article>();
   model.CurrentPage = GetContextItem<WebPage>();

   return View(model);
}

Make sure that your Controller inherits from GlassController.

Obviously with that you will need to create a new View Model that has two properties. And then in your view you would have something like this:

@model ArticleViewModel

@* Pull Article Datasource Data *@
@Html.Glass().Editable(Model.Article, x => x.Tittle)

@* Pull WebPage Data *@
@Html.Glass().Editable(Model.CurrentPage, x => x.Description)
  • Can we do it just on ViewRendering by adding property public WebPage page = new SitecoreContext().GetItem<CommonPage>(id); to Article and pull it into views.Is this maped? – Đặng Tú May 31 '17 at 8:32
  • You can do that as well in the View Rendering. But I will note, that's generally not the best way to do that. – Dylan Young May 31 '17 at 13:14

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.