简体   繁体   English

从Blogpost ViewModel获取父项-Sitefinity

[英]Get parent item from blogpost viewmodel - Sitefinity

I need to get the parent (blog) item in the razor view from the viewmodel object for any given blog post. 我需要从任何给定博客文章的viewmodel对象的razor视图中获取父项(博客)。

I'm doing this in the standard List.BlogPostList.cshtml file. 我在标准List.BlogPostList.cshtml文件中执行此操作。

More specifically, I need either the set URL or title. 更具体地说,我需要设置URL或标题。 Since the controller is wrapped up in the resources for this application, I don't have direct access to it. 由于控制器包含在此应用程序的资源中,因此我没有直接访问它的权限。


Tried 试着

item.ParentItem().DataItem.GetDefaultUrl()

Got this in the error log: 在错误日志中得到了这个:

Could not find a parent item property for the given item. 找不到给定项目的父项目属性。 This extension method should only be used for accessing a parent item of DynamicContent items. 此扩展方法仅应用于访问DynamicContent项目的父项目。


Tried 试着

item.GetRelatedParentItems("Blog").ToString()

Got this in the error log: 在错误日志中得到了这个:

Type "Blog" cannot be resolved. 类型“博客”无法解析。

Looks like that's close, but I cannot guess as to the parentItemsTypeName magic string it wants, and of course the Sitefinity documentation is attrocious. 看起来已经接近了,但是我无法猜到它想要的parentItemsTypeName魔术字符串,当然,Sitefinity文档也很糟糕。

Something like this should work: 这样的事情应该起作用:

@foreach (var item in Model.Items)
{
       var parent = (item.DataItem as BlogPost).Parent;
       var parentTitle = parent.Title;
       var parentUrl = parent.UrlName;
}

Make sure to add the following using clauses as well: 确保还添加以下using子句:

@using Telerik.Sitefinity.Blogs.Model;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM