2

我已经搜索并尝试了 2 天来更改显示在我的主页上的 RecentBlogPosts 内容类型的视图。我想显示帖子中的标题和短语。

我设法找到了一个显示每个帖子标题的视图,但我还没有弄清楚模型中其他部分的名称或如何提取文本。

帮助将不胜感激!

@using Orchard.ContentManagement;
@using Orchard.Core.Routable.Models;
@using Contrib.Hyperlink.Fields;
@{


  IEnumerable<object> blogPosts = Model.ContentItems.ContentItems;
}
@if (blogPosts == null || blogPosts.Count() < 1)
{
    <p>@T("No posts.")</p>
}
else
{
   <div class="content-items">
   @foreach (dynamic post in blogPosts)
   {
            string title = post.Title;
            ContentItem item = post.ContentItem;

   <div class="blogpost" style="width: 300px; padding-left: 15px; float: left;">
         <p class="content-item-summary">@Html.ItemDisplayLink(title, item)</p>
   </div>

   }
   </div>
}

上面的代码(主要)来自Bertrand Le Roy博客,对此我表示感谢。

4

2 回答 2

1

ContentItem 是一个BlogPost内容项,其中包含BlogPostPart(来自 Orchard.Blogs.Models 命名空间)。所以你可以使用它拥有的任何属性——查看我上面提供的源代码的链接。

This part has the Text property, which returns the Html from corresponding BodyPart (which BlogPost type has attached too) under the hood. And btw - Remember to use @Html.Raw(part.Text) to display the fetched content.

One more thing - for making life easier I'd rather cast that iterated objects to BlogPostPart to get Intellisense:

@foreach (var post in blogPosts.Select(p => ((IContent)p.ContentItem).As<BlogPostPart>())) 
{ 
    ... 
}

HTH

于 2011-04-02T14:18:28.247 回答
1

You might want to try the shape tracing feature in Orchard 1.1 (due next week). The model tab in there will enable you to dig into the objects and figure out the syntax to display pretty much anything.

于 2011-04-06T17:58:13.767 回答