设置
模型
virtual
是玻璃地图所要求的。
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; }
}
}
视图
被Sitecore引用为视图呈现,模型指向Sample.Parent
(参见下面的Sitecore模型定义)。
@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模型定义
这里我不确定我做得对不对。这些是我定义为Sitecore模型的模型类型(在/sitecore/layout/models
下)。
Sample.Parent`1[T], Sample
也曾尝试过(但未成功):- `Sample.Parent, Sample`
- `Sample.Parent`1[Sample.Article, Sample], Sample`
- `Sample.Parent<Sample.Article>, Sample`)
Sample.Article, Sample
Sample.Teaser, Sample
这个是可能的吗?
示例代码是简化的,但是应该能够捕捉到我想要做的事情。基本上,我希望能够使用泛型类型来重用更多的代码。由于外部的限制,我只能使用Glass 3。我看到的错误要么是Sitecore找不到类型,要么是“对象引用没有设置”(出现这种情况时,它似乎使用Sitecore.Mvc.Presentation.RenderingModel, Sitecore.Mvc
作为模型)。
还是我疯了?:)有更好的方法来完成这个任务吗?
发布于 2015-07-28 11:49:37
我认为格拉斯试图处理泛型字符串的方式可能存在困难(老实说,我从来没有设计它来处理泛型字符串)。
如果您使用的是V4,那么您就不需要在Sitecore中定义一个模型。保留“模型”字段为空,格拉斯应从cshtml文件中的@inherits减速解析该模型。
https://stackoverflow.com/questions/31652030
复制相似问题