c# - Using a generic type in a Sitecore view rendering with Glass Mapper -
setup
models
pocos, virtual
required glass mapper.
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
referenced sitecore view renderings, model pointing sample.parent
(see below 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 definitions
here's i'm not sure if did right. these model types defined sitecore models (under /sitecore/layout/models
).
sample.parent`1[t], sample
also tried (without success):
sample.parent, sample
sample.parent`1[sample.article, sample], sample
sample.parent<sample.article>, sample
)
sample.article, sample
sample.teaser, sample
is possible?
the example code simplified, should capture i'm trying do. want able use generic type way reuse more code. because of external restrictions i'm unable use glass mapper 3. errors i'm seeing either sitecore cannot find type, or "object reference not set" (it appears use sitecore.mvc.presentation.renderingmodel, sitecore.mvc
model when happens).
or being crazy? :) there better way accomplish this?
i think there difficulties in way glass tries process generic string (to honest never designed handle generic strings).
if using v4 don't need define model in sitecore. leave model field blank , glass should resolve model @inherits deceleration in cshtml file.
Comments
Post a Comment