Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Sunil 14 posts 84 karma points
    Feb 13, 2020 @ 12:59
    Sunil
    0

    Code Error

    public List

            var subPages = page.Children.Where("IsVisible"); //Error Line
          }
    

    in the above "Error Line" 'Where' gives below error in Umbraco version 8:

    'object' does not contain a definition for 'Where'

    Please suggest solution

  • Kevin Jump 1867 posts 11859 karma points MVP 4x c-trib
    Feb 13, 2020 @ 13:07
    Kevin Jump
    0

    Hi,

    In Umbraco 8 I don't think you can do it with a string value anymore :(

    however :

     var subPages =  pages.Children.Where(x => x.IsVisible());
    

    should work.

  • Sunil 14 posts 84 karma points
    Feb 13, 2020 @ 13:16
    Sunil
    0

    No, It did not work; even not compiling. Below is the whole code:

    public List

  • Kevin Jump 1867 posts 11859 karma points MVP 4x c-trib
    Feb 13, 2020 @ 13:32
    Kevin Jump
    0

    Hi,

    It looks like most of the code is missing from that last reply :(

    when you paste it into the forum, you may need to press the code sample button "{}" at the top to format it.

  • Sunil 14 posts 84 karma points
    Feb 13, 2020 @ 13:40
    Sunil
    0

    Below is the code

    public List<NavigationList> GetSubNavigationList() { IPublishedContent page = Umbraco.Content(pageId); var subPages = page.Children.Where("IsVisible");//Error Line }

  • Kevin Jump 1867 posts 11859 karma points MVP 4x c-trib
    Feb 13, 2020 @ 13:54
    Kevin Jump
    0

    Hi

    Assuming that NavigationList is a Model from Models builder?

    then the following should work:

    public List<NavigationList> GetSubNavigationList()
    {
        IPublishedContent page = Umbraco.Content(pageId);
        var subPages = page.Children.Where(x => x.IsVisible());
    
        return subPages.Select(x => new NavigationList(x)).ToList();
    }
    

    you will need to add

    using Umbraco.Web; 
    

    to the top of the file, if it's not already there.

  • Sunil 14 posts 84 karma points
    Feb 13, 2020 @ 15:00
    Sunil
    0

    Actually, "IsVisible" is property id of a Document type named "ShowHide" that has "true/false" value. And it has been placed as a composition on other document type. So, i am trying to hide those menus having "IsVisible" set to false. So the code page.Children.Where(x => x.IsVisible()) is giving compile time error where as the code page.Children.Where("IsVisible") is being compiled .

  • Kevin Jump 1867 posts 11859 karma points MVP 4x c-trib
    Feb 13, 2020 @ 16:17
    Kevin Jump
    0

    Hi,

    yeah, there is an internal IsVisible method on the content so I think that's where its getting confused (it doesn't know if you want the property on the element or the method) - The Internal IsVisible looks for a Umbraco property called UmbracoNaviHide - that might well be confusing it.

    because of this, you might have to get the value by name and test it

    maybe this:

    var subPages = page.Children.Where(x => x.Value<bool>("IsVisible") == false));
    
  • Sunil 14 posts 84 karma points
    Feb 14, 2020 @ 05:59
    Sunil
    0

    Coorect, IsVisible property id is same as UmbracoNaviHide. The codevar subPages = page.Children.Where(x => x.Value<bool>("IsVisible") == false)); is giving compilation error for expression .Where(x => x.Value<bool>("IsVisible") == false)

  • Sunil 14 posts 84 karma points
    Feb 19, 2020 @ 10:30
    Sunil
    0

    Below is the entire code that i have written in MVC5 controller. Please read the comment line in code below `using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using Umbraco.Web.Mvc; using Website.Models; using Umbraco.Core.Models; using Umbraco.Core.Models.PublishedContent; using Umbraco.Web;

    namespace Website.Controllers { public class LayoutController : SurfaceController { private const string PATH = "~/Views/Partials/Layout/";

        [ChildActionOnly]
        public ActionResult RenderHeader()
        {
            //return PartialView(string.Format("{0}_Header.cshtml", PATH));
            List<NavigationList> nav = GetNavigationModel();
            return PartialView((string.Format("{0}_Header.cshtml", PATH)), nav);
        }
    
        [ChildActionOnly]
        public ActionResult RenderFooter()
        {
            return PartialView(string.Format("{0}_Footer.cshtml", PATH));
        }
    
        public List<NavigationList> GetNavigationModel()
        {
            int pageId = int.Parse(CurrentPage.Path.Split(',')[1]);
            IPublishedContent pageInfo = Umbraco.Content(pageId);
            var nav = new List<NavigationList>()
            {
                new NavigationList(new NavigationLinkInfo(pageInfo.Url,pageInfo.Name))
            };
            nav.AddRange(GetSubNavigationList(pageInfo));
            return nav;
        }
    
        public List<NavigationList> GetSubNavigationList(dynamic page)
        {
            List<NavigationList> navList = null;
            var subPages = page.Children;//.Where("IsVisible"); Here if i remove comment it shows error: Object does not have where definition
    
            if (subPages != null)
            {
                navList = new List<NavigationList>();
                foreach (var subPage in subPages)
                {
                    var listItem = new NavigationList(new NavigationLinkInfo(subPage.Url, subPage.Name))
                    {
                        NavItems = GetSubNavigationList(subPage)
                    };
                    navList.Add(listItem);
                }
            }
            return navList;
        }
    }
    

    }`

  • Kevin Jump 1867 posts 11859 karma points MVP 4x c-trib
    Feb 19, 2020 @ 11:51
    Kevin Jump
    0

    Hi,

    couple of issues but. to fix it i think?

    public List<NavigationList> GetSubNavigationList(dynamic page)
    

    should read

    public List<NavigationList> GetSubNavigationList(IPublishedContent page)
    

    then you will have a PublishedContent item, so you can do .

    var subPages = page.Children.Where(x => x.Value<bool>("IsVisible") == false));
    

    this should work when pages isn't a dynamic object but a IPublishedContent item.

    Also. if you want the root of the site, CurrentPage.Root() is probably a better way to get it than the path.split stuff.

  • Sunil 14 posts 84 karma points
    Feb 19, 2020 @ 14:16
    Sunil
    0

    I applied the code var subPages = page.Children.Where(x => x.Value("IsVisible") == false)); but it did not work and show below message at compile time:

    "Can not use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type."

  • Kevin Jump 1867 posts 11859 karma points MVP 4x c-trib
    Feb 19, 2020 @ 15:01
    Kevin Jump
    0

    Hi,

    the below does compile, maybe you where missing a using statement or didn't change the parameter in the function from dynamic to IPublishedContent ?

    Following using statements :

    using System.Collections.Generic;
    using System.Linq;
    using Umbraco.Core.Models.PublishedContent;
    using Umbraco.Web;
    using Umbraco.Web.Mvc;
    

    Super stripped down function:

    public class TestControllerThing: SurfaceController
    {
        public List<NavigationList> GetSubNavigationList(IPublishedContent page)
        {
            List<NavigationList> navList = null;
    
            // Added default value, so if it's not set, do you want it visible or not?
            // here its set to true.
            var subPages = page.Children
                .Where(x => x.Value("IsVisible", defaultValue: true) == true);
    
            // other code removed for brevity......
    
            return navList;
        }
    }
    
  • Sunil 14 posts 84 karma points
    Feb 19, 2020 @ 16:36
    Sunil
    0

    The code var subPages = page.Children .Where(x => x.Value("Visible", defaultValue: true) == true); is being compiled and running. But, I am using "Visible" for UmbracoNaviHide(True/false) that is in a document type and using it as a composition. But it is hiding all menus except Parent; irrespective of their value(true/false).

  • Kevin Jump 1867 posts 11859 karma points MVP 4x c-trib
    Feb 19, 2020 @ 16:41
    Kevin Jump
    0

    Hi

    if you are using UmbracoNaviHide then the x.Value("Visible") should be replaced with x.IsVisible() (sorry i thought eariler you said there was a custom attribute).

    so the where line becomes

    .Where(x => x.IsVisible())
    

    IsVisible() is a wrapper around x.Value("umbracoNaviHide") so its about the property name really

  • Sunil 14 posts 84 karma points
    Feb 20, 2020 @ 15:33
    Sunil
    0

    Thank you very much Kevin. It is working fine now.

  • Sunil 14 posts 84 karma points
    Feb 25, 2020 @ 13:56
    Sunil
    0

    Hi,

    I am writing code @{ var selection = CurrentPage.Ancestors(); } in a view(.cshtml file). But it show compile time error "The name CurrentPage does not exits in the current context". Please suggest. I am using Umbraco8.

  • Kevin Jump 1867 posts 11859 karma points MVP 4x c-trib
    Feb 25, 2020 @ 13:58
    Kevin Jump
    0

    Hi

    try

    var selection = Model.Ancestors(); 
    
  • Sunil 14 posts 84 karma points
    Feb 25, 2020 @ 14:10
    Sunil
    0

    It worked but below is the entire code to implement breadcrumb:

    ` @{ var selection = Model.Ancestors(); if (selection.Any()) { foreach(var page in selection.OrderBy("Level")) { @page.Name }

            }
        }`
    

    Now its showing compile time error at OrderBy, @page.Url & page.Name

  • Kevin Jump 1867 posts 11859 karma points MVP 4x c-trib
    Feb 25, 2020 @ 14:19
    Kevin Jump
    0

    Hi

    You are working with Typed Content in Umbraco 8 - so everything is an IPublishedContent item.

    so the orderBy would be need to be using Lambda expressions

    var selection = Model.Ancestors();
    
    if (selection.Any())
    {
        foreach (var page in selection.OrderBy(x => x.Level))
        {
            @page.Name
        }
    }
    
  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies