Umbraco将"IPublishedContent"类型转换为"CustomModel"类型

har*_*amc 3 c# asp.net-mvc model umbraco

我正在测试Umbraco并尝试在页面上设置博客列表.我有一个自定义BlogMode.cs,如下所示:

public class BlogPostModel : RenderModel
{
    public BlogPostModel() : base(UmbracoContext.Current.PublishedContentRequest.PublishedContent)
    {
    }

    public string MainBlogImage { get; set; }
    public string ImageAltText { get; set; }
    public string Introduction { get; set; }
    public string Category { get; set; }
    public IEnumerable<IPublishedContent> BlogPosts { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

这样可以正常工作,因为我的模型也没有上面的属性.现在我要做的是通过使用以下内容获取所有博客文章:

var posts = Model.Content.Children.ToList();
Run Code Online (Sandbox Code Playgroud)

但这样做会创建一个类型列表IPublishedContent.所以现在我不能在我的博客模型中使用Model.Introduction或者Model.ImageAltText任何其他属性,因为它是类型的IPublishedContent

我如何获得这个集合并使其成为类型BlogModel

Mor*_*rph 7

不确定你使用的是什么版本的Umbraco,但我使用的是7.4.3(包括模型构建器),这对我有用.

var page = umbracoHelper.TypedContent(relatedLink.Internal);
if (page.ContentType.Alias.Equals("editorial"))
{
    var typedPage = (Editorial)page;
    //Your code here, for example 'typedPage.SiteLinkImage'
}
Run Code Online (Sandbox Code Playgroud)

'Editorial'类由模型构建器生成.