Copied to clipboard

Flag this post as spam?

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


  • Christina 127 posts 390 karma points notactivated
    Apr 27, 2018 @ 09:38
    Christina
    0

    Navigation List Error from Youtube vide How to Build ..Part 5

    Hi I'm following Paul Seal and struggeling with a navigationlist https://www.youtube.com/watch?v=zRDHK_isHCQ I have I SiteLayoutController and render it to an Partial View and i get an Error CS1660 Cannot convert lambda expression to type 'string' because it is not a delegate type.

    I have changed from Model to currentpage but get an error on x.DocumentTypeAlias I have tested to inster using Umbraco.Web but it dosen't help

    Can someone help me!

    The Code IPublishedContent homePage = CurrentPage.AncestorOrSelf(1).DescendantsOrSelf().Where(x => x.DocumentTypeAlias == "Home").FirstOrDefault();

            List<NavigationListItem> nav = new List<NavigationListItem>();
            nav.Add(new NavigationListItem(new NavigationLink(homePage.Url, homePage.Name)));
            nav.AddRange(GetChildNavigationList(homePage));
            return nav;
    
  • Mark 91 posts -18 karma points
    Apr 27, 2018 @ 09:43
    Mark
    0

    The issue is with your Where clause.

    The error you get is Error CS1660 Cannot convert lambda expression to type 'string' meaning your expression x => x.DocumentTypeAlias == "Home" is not a string.

    Try ..Where("DocumentTypeAlias == \"Home\"")

  • Christina 127 posts 390 karma points notactivated
    Apr 27, 2018 @ 10:41
    Christina
    0

    Thanks! But it dosen't work se error below

    If I use this code it works, but i prefer the other one
    const int HOMEPAGEPOSITIONINPATH = 1; int homePageId = int.Parse(CurrentPage.Path.Split(',')[HOMEPAGEPOSITIONINPATH]); IPublishedContent homePage = Umbraco.Content(homePageId);

    Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

    Source Error:

    Line 47: Line 48: List

  • Mark 91 posts -18 karma points
    Apr 27, 2018 @ 11:00
    Mark
    0

    If you post the entire code here I will take a look.

  • Christina 127 posts 390 karma points notactivated
    Apr 27, 2018 @ 11:41
    Christina
    0

    Thanks a lot Here is my code

    /C

    //Masterpage @inherits Umbraco.Web.Mvc.UmbracoTemplatePage @using System.Web.Optimization @{ Layout = null; }

    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <link href="~/css/styles.min.css" rel="stylesheet" />
    @RenderSection("Head",false)
    

    @{Html.RenderAction("RenderHeader", "SiteLayout");} 
    @{Html.RenderAction("RenderHero", "SiteLayout");}
    <div class="container">
        @RenderBody()
        @{Html.RenderAction("RenderFooter", "SiteLayout");}
    </div>
    @RenderSection("ScriptsBottom", false)
    

    //SiteLayoutController using System; using System.Collections.Generic; using System.Web.Mvc; using Umbraco.Web.Mvc; using Umbraco.Web; using Umbraco.Core.Models; using konferens.Web.Models; using System.Runtime.Caching; using System.Linq;

    namespace konferens.Web.Controls { public class SiteLayoutController:SurfaceController { private const string PARTIALVIEWFOLDER = "~/Views/Partials/SiteLayout/";

        public ActionResult RenderFooter()
        {
            return PartialView(PARTIAL_VIEW_FOLDER + "_Footer.cshtml");
        }
        /// <summary>
        /// Renders the top navigation in the header partial
        /// </summary>
        /// <returns>Partial view with a model</returns>
        public ActionResult RenderHeader()
        {
            //List<NavigationListItem> nav = GetNavigationModelFromDatabase();
            List<NavigationListItem> nav = GetObjectFromCache<List<NavigationListItem>>("mainNav", 5, GetNavigationModelFromDatabase);
            return PartialView(PARTIAL_VIEW_FOLDER + "_Header.cshtml", nav);
        }
        public ActionResult RenderHero()
        {
            return PartialView(PARTIAL_VIEW_FOLDER + "_Hero.cshtml");
        }
    
        /// <summary>
        /// Finds the home page and gets the navigation structure based on it and it's children
        /// </summary>
        /// <returns>A List of NavigationListItems, representing the structure of the site.</returns>
        private List<NavigationListItem> GetNavigationModelFromDatabase()
        {
    
            const int HOME_PAGE_POSITION_IN_PATH = 1;
            int homePageId = int.Parse(CurrentPage.Path.Split(',')[HOME_PAGE_POSITION_IN_PATH]);
            IPublishedContent homePage = Umbraco.Content(homePageId);
            //IPublishedContent homePage = CurrentPage.AncestorOrSelf(1).DescendantsOrSelf().Where("DocumentTypeAlias == \"Home\"").FirstOrDefault();
    
            List<NavigationListItem> nav = new List<NavigationListItem>();
            nav.Add(new NavigationListItem(new NavigationLink(homePage.Url, homePage.Name)));
            nav.AddRange(GetChildNavigationList(homePage));
            return nav;
        }
    
        /// <summary>
        /// Loops through the child pages of a given page and their children to get the structure of the site.
        /// </summary>
        /// <param name="page">The parent page which you want the child structure for</param>
        /// <returns>A List of NavigationListItems, representing the structure of the pages below a page.</returns>
        private List<NavigationListItem> GetChildNavigationList(dynamic page)
        {
            List<NavigationListItem> listItems = null;
            var childPages = page.Children.Where("Visible");
            if (childPages != null && childPages.Any() && childPages.Count() > 0)
            {
                listItems = new List<NavigationListItem>();
                foreach (var childPage in childPages)
                {
                    NavigationListItem listItem = new NavigationListItem(new NavigationLink(childPage.Url, childPage.Name));
                    listItem.Items = GetChildNavigationList(childPage);
                    listItems.Add(listItem);
                }
            }
            return listItems;
        }
    
        /// <summary>
        /// A generic function for getting and setting objects to the memory cache.
        /// </summary>
        /// <typeparam name="T">The type of the object to be returned.</typeparam>
        /// <param name="cacheItemName">The name to be used when storing this object in the cache.</param>
        /// <param name="cacheTimeInMinutes">How long to cache this object for.</param>
        /// <param name="objectSettingFunction">A parameterless function to call if the object isn't in the cache and you need to set it.</param>
        /// <returns>An object of the type you asked for</returns>
        private static T GetObjectFromCache<T>(string cacheItemName, int cacheTimeInMinutes, Func<T> objectSettingFunction)
        {
            ObjectCache cache = MemoryCache.Default;
            var cachedObject = (T)cache[cacheItemName];
            if (cachedObject == null)
            {
                CacheItemPolicy policy = new CacheItemPolicy();
                policy.AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(cacheTimeInMinutes);
                cachedObject = objectSettingFunction();
                cache.Set(cacheItemName, cachedObject, policy);
            }
            return cachedObject;
        }
    }
    

    }

    //NavigationLink.cs namespace konferens.Web.Models { public class NavigationLink { public string Text { get; set; } public string Url { get; set; } public bool NewWindow { get; set; } public string Target { get { return NewWindow ? "_blank" : null; } } public string Title { get; set; }

        public NavigationLink()
        { }
    
        public NavigationLink(string url, string text = null, bool newWindow = false, string title = null)
        {
            Text = text;
            Url = url;
            NewWindow = newWindow;
            Title = title;
        }
    }
    

    }

    //NavigationListItem.cs using System.Collections.Generic; using System.Linq;

    namespace konferens.Web.Models { public class NavigationListItem { public string Text { get; set; } public NavigationLink Link { get; set; } public List

        public NavigationListItem()
        { }
    
        public NavigationListItem(NavigationLink link)
        {
            Link = link;
        }
    
        public NavigationListItem(string text)
        {
            Text = text;
        }
    }
    

    } //Header PartialView @inherits Umbraco.Web.Mvc.UmbracoViewPage<>

    @helper RenderChildItems(List

  • @if (!String.IsNullOrEmpty(item.Text)) { @item.Text } else if (item.Link != null) { @item.Link.Text }

                    @if (item.HasChildren)
                    {
                        <ul class="sub-menu">
                            @RenderChildItems(item.Items)
                        </ul>
                    }
                </li>
            }
        }
    

    }

Copy Link
  • Christina 127 posts 390 karma points notactivated
    Apr 27, 2018 @ 12:08
    Christina
    0

    Hi again

    I have a root node - level 1 Home Page1 Page2 -level2

    Copy Link
  • Mark 91 posts -18 karma points
    Apr 27, 2018 @ 15:12
    Mark
    0

    Hi

    I am replying here as your code broke the topic, not sure how!

    I would say that instead of trying to find Home and then listing the children, use Umbraco.TypedContentAtRoot(); and list the Children, try to stay away from DescendantsOrSelf as that may return more pages than you need in the navigation.

    Also, try and use IsVisible or Visible for those pages that are published.

    Thanks

    Copy Link
  • Please Sign in or register to post replies

    Write your reply to:

    Draft