可以从foreachLoop Umbraco C#获取数据单个数据

时间:2019-05-28 11:25:11

标签: c# umbraco umbraco7 umbraco-blog

我的代码工作正常,但是当我尝试获取每个资金的数据= subpage.Name时,我得到的所有资金数据都不是单独的。我在这里想念什么?? 谁能帮我?对不起,我刚接触umbraco和c#,什么是管理此问题的最佳方法

现在这样

(16) [{…}, {…},]...
0: {Fundo: "CURTO PRAZO", data: Array(21)}
1: {Fundo: "CA MONETARIO", data: Array(21)}
...

In reality it should be

(16) [{…}, {…},]...
0: {Fundo: "CURTO PRAZO", data: Array(4)}
1: {Fundo: "CA MONETARIO", data: Array(3)}
for example
...

这是我的代码

    var currentPage = Model.Content;

    var selection = Umbraco.Content(1448);

    @foreach (var page in selection.Children){

    if(page.Children.Count() > 0){ 

    foreach(var subpage in page.Children){

    var listaPdf = subpage.GetPropertyValue<IEnumerable<IPublishedContent>>("listaPdfsFundos");

         if(listaPdf == null) {

         listaPdf = new List<IPublishedContent>();}

        if(listaPdf != null){

        foreach(var row in listaPdf) {

        name = Umbraco.Field(row, "categoriaDoFundo").ToString();

        var id = Umbraco.Field(row, "documentos").ToString();

        var mediaItem = Umbraco.TypedMedia(id);

        url = mediaItem.Url;

        <script type="text/javascript">

       data.push({"Nome":"@name", "Url":"@url"}); 

        </script> 
         }

         <script type="text/javascript">

        list.push({"Fundo":"@subpage.Name", data }); 
        </script> 

              }

            }

        }

     }

1 个答案:

答案 0 :(得分:1)

您应该使用UmbracoHelper来获取IPublishedContent,然后才能获取内容页的子级并访问各个属性。

请在下面查看详细信息;在我的示例中,我的测试内容ID是首页的nodeId,并查看如何获取首页的子级和第一个子级的属性-因此请根据此更新您的代码,有关更多详细信息,请使用a look at this;

            var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
            var testContentId = 1353; // This is your umbraco node id of your content page
            var publishedContent = umbracoHelper.TypedContent(testContentId);

            if (publishedContent != null)
            {
                foreach (var child in publishedContent.Children)
                {
                    // This is where you can reach the Children of this child as well as individual properties of this child
                    if (child.HasProperty("emailAddress_Contact_Information"))
                    {
                        var emailAddressContactInformation = child.GetProperty("emailAddress_Contact_Information");
                    }
                }
            }

enter image description here