简体   繁体   English

如何将Umbraco子节点设置为列表<T>

[英]How to set Umbraco Child Node to List<T>

I have a blog export package which exports blog content in Umbraco to XML. 我有一个博客导出程序包,可以将Umbraco中的博客内容导出到XML。

Now I want to export comment data, the comments section is set as a childNode on the NewsItem node, how can I use this format to grab the data from the childNode into the list? 现在我要导出评论数据,评论部分在NewsItem节点上设置为childNode,如何使用这种格式将数据从childNode抓取到列表中?

Here is my code: 这是我的代码:

public List<BlogPosts> getPostList()
{
    var contentType = ApplicationContext.Current.Services.ContentTypeService
        .GetContentType("umbNewsItem");
    var nodes = ApplicationContext.Current.Services.ContentService
        .GetContentOfContentType(contentType.Id).Select(content => new Node(content.Id));

    return nodes.Select(node => new BlogPosts()
    {
        Title = node.GetProperty("title").ToNullSafeString(),
        BodyText = node.GetProperty("bodyText").ToNullSafeString(),
        PublishDate = node.GetProperty("publishDate").ToNullSafeString(),
        Author = node.GetProperty("author").ToNullSafeString(),
        Image = node.GetProperty("image").ToNullSafeString(),
        //This is where I want to grab the blog comments content
        Comments = node.ChildrenAsList.Add("comments") 
    }).ToList();
}

My first attempt with this, I get an error on the .Add("comments") line which reads: 我的第一次尝试,在.Add(“ comments”)行上收到一条错误消息,内容为:

The best overloaded method match for 'System.Collections.Generic.List<umbraco.interfaces.INode>.Add(umbraco.interfaces.INode)' has some invalid arguments

the next thing I tried was this: 接下来我尝试的是:

Comments = node.ChildrenAsList<BlogComment>.Add("comments").ToList()

which returns the following error: 返回以下错误:

The property 'umbraco.NodeFactory.Node.ChildrenAsList' cannot be used with type arguments

I have also tried this: 我也尝试过这个:

Comments = node.ChildrenAsList.Add("comments").ToList()

which returned this error: 返回此错误:

The best overloaded method match for 'System.Collections.Generic.List<umbraco.interfaces.INode>.Add(umbraco.interfaces.INode)' has some invalid arguments

This is my BlogPosts model: 这是我的BlogPosts模型:

public class BlogPosts
{
    public string Title { get; set; }
    public string BodyText { get; set; }
    public string PublishDate { get; set; }
    public string Author { get; set; }
    public string Image { get; set; }
    public List<BlogComment> Comments { get; set; }
}

public class BlogComment
{
    public string Comment { get; set; }
    public string CommentDate { get; set; }
}

This is an example of the Umbraco backoffice page: Image 这是Umbraco后台页面的示例: 图片

I've searched throughout stackoverflow and google for anything which refers to calling data from a childNode into a list but the list type here is INode, when using this: 我在整个stackoverflow和google中搜索了任何内容,这些内容是指使用childNode将数据从childNode调用到列表中,但此处的列表类型是INode:

Comments = node.ChildrenAsList

it returns this error: 它返回此错误:

Cannot implicitly convert type 'System.Collections.Generic.List<umbraco.interfaces.INode>' to 'System.Collections.Generic.List<UmbracoBlogsExportPackage.Models.BlogComment>'

Okay then :-) 好吧 :-)

  • First of all, .Add() tries to add something to a collection, so that won't work here. 首先,.Add()尝试向集合中添加内容,因此在这里不起作用。

  • Second, I think selecting Content as Nodes is a bit backwards, so I would try not to do that. 其次,我认为选择“内容作为节点”有点倒退,所以我尽量不要这样做。

  • Third, IEnumerable have a Cast() method that I think might work here. 第三,IEnumerable有一个Cast()方法,我认为这可能在这里起作用。 I can't really test it, though. 不过,我无法真正测试它。

Again, this is very untested, but maybe try something like this? 再次,这是未经测试的,但也许尝试这样的事情? Obviously I don't know the Comment DocType alias, so remember to change that bit :-) 显然我不知道Comment DocType别名,因此请记住更改该位:-)

public List<BlogPosts> getPostList()
{
    var contentType = UmbracoContext.Current.Application.Services.ContentTypeService
        .GetContentType("umbNewsItem");
    var contentService = UmbracoContext.Current.Application.Services.ContentService;
    var nodes = contentService.GetContentOfContentType(contentType.Id);

    return nodes.Select(node => new BlogPosts()
    {
        Title = node.GetValue("title").ToNullSafeString(),
        BodyText = node.GetValue("bodyText").ToNullSafeString(),
        PublishDate = node.GetValue("publishDate").ToNullSafeString(),
        Author = node.GetValue("author").ToNullSafeString(),
        Image = node.GetValue("image").ToNullSafeString(),
        //This is where I want to grab the blog comments content
        Comments = contentService.GetChildren(node.Id).Where(x => x.ContentType.Alias == "Comment").Cast<BlogComment>().ToList()
    }).ToList();
}

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

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