Using a Generic Type in a Sitecore View with Glass Mapper

Customization

Models

POCOs, virtual

Glass Mapper required.

using System.Collections.Generic;
using Glass.Mapper.Sc.Configuration.Attributes;
using Glass.Mapper.Sc.Fields;

namespace Sample
{
    public class Parent<T>
    {
        [SitecoreId]
        public virtual Guid Id { get; set; }
        public virtual string Title { get; set; }
        public virtual IEnumerable<T> Children { get; set; }
    }

    public class Article
    {
        [SitecoreId]
        public virtual Guid Id { get; set; }
        public virtual string Title { get; set; }
        public virtual string Text { get; set; }
    }

    public class Teaser
    {
        [SitecoreId]
        public virtual Guid Id { get; set; }
        public virtual string Title { get; set; }
        public virtual Image Banner { get; set; }
    }
}

      

Views

Sitecore reference as a rendering of a view with a model pointing to Sample.Parent

(see below for Sitecore model definitions).

@inherits Glass.Mapper.Sc.Web.Mvc.GlassView<Sample.Parent<Sample.Article>>

<h1>@Editable(x => x.Title)</h1>
<div class="article-list">
    @foreach (var article in Model.Children)
    {
        <article class="article">
            <h2 class="article-title">@Editable(article, x => x.Title)</h2>
            <div class="article-content">@Editable(article, x => x.Text)</div>
        </article>
    }
</div>

      

@inherits Glass.Mapper.Sc.Web.Mvc.GlassView<Sample.Parent<Sample.Teaser>>

<h1>@Editable(x => x.Title)</h1>
<div class="teaser-list">
    @foreach (var teaser in Model.Children)
    {
        <article class="teaser">
            <h2 class="teaser-title">@Editable(teaser, x => x.Title)</h2>
            <div class="teaser-banner">@RenderImage(teaser, x => x.Banner)</div>
        </article>
    }
</div>

      

Sitecore Model Definition

Here's where I'm not sure if I did it right. These are the models that I have defined as Sitecore models (under /sitecore/layout/models

).

  • Sample.Parent`1[T], Sample

    Also tried (no success):

    • Sample.Parent, Sample

    • Sample.Parent`1[Sample.Article, Sample], Sample

    • Sample.Parent<Sample.Article>, Sample

      )
  • Sample.Article, Sample

  • Sample.Teaser, Sample

Is it possible?

The sample code is simplified, but should capture what I am trying to do. Basically, I want to be able to use a generic type as a way to reuse more code. Due to external constraints, I cannot use anything other than Glass Mapper 3. The errors I see are either that Sitecore cannot find the type or "object reference not set" (it appears to be using Sitecore.Mvc.Presentation.RenderingModel, Sitecore.Mvc

as a model when it happens).

Or am I crazy? :) Is there a better way to do this?

+3


source to share


1 answer


I think there is probably a difficulty in how Glass tries to handle a generic string (to be honest, I never designed it to handle typical strings).



If you are using V4, you do not need to define the model in Sitecore. Leave the model field blank and Glass should resolve the model from @inherits easing in the cshtml file.

+1


source







All Articles