Copied to clipboard

Flag this post as spam?

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


  • Simon Cropper 3 posts 85 karma points
    Apr 04, 2017 @ 08:38
    Simon Cropper
    0

    How to set URL of a content page to point to the home page

    Hi,

    I'm fairly new to Umbraco and in the process of creating my first site. I'm using c# to create a model of my nav structure then using razor to loop through the model to render the nav items. It's all working fine.

    However, I have a "Services" nav item under "Home" (which has several children) for which a want the href URL to remain as a '#' i.e. I don't want to create a Services page - I simply want the visitor to be able to select the sub-menu items.

    I have a doc type of Services which uses a template called "Services".

    How can I configure the URL for the "Services" page so the href is set to a '#'?

  • Alex Skrypnyk 5908 posts 22603 karma points MVP 4x admin c-trib
    Apr 04, 2017 @ 09:07
    Alex Skrypnyk
    0

    Hi Simon

    You have to detect in code where you are on Services page and insert # in place where should be url.

    Can you share code of the nav? We will find out how to fix it.

    Thanks,

    Alex

  • Simon Cropper 3 posts 85 karma points
    Apr 04, 2017 @ 09:24
    Simon Cropper
    1

    Hi Alex,

    Thanks for the prompt response. I had the same idea (and as you can see from the code have tried) - but I also wondered if there is a more elegant, back-end solution I could use rather than hard coding something.

    I try setting @item.Link.Url = "/"; - but the datatype for attribute Link is 'Link' - and, as I'm trying to set it to a string value, the following error is generated:

    "Invalid expression term '='"

    So, I guess my question is - what syntax should I us to programmatically set the href value for my Services page to '#'?

    Here is the code:

    @helper RenderChildItems(List<NavigationListItemModel> listItems)
    {
    if (listItems != null)
    
    {
        foreach (var item in listItems)
        {
                if (item.HasChildren)
                {
                    <li class="nav-item dropdown">
                        @* Menu 1 *@
                        <a href="@item.Link.Url" class="nav-link py-0 sliding-u-l-r @(Umbraco.AssignedContentItem.Url == item.Link.Url ? "active" : null)"
                           >@item.Link.Text</a>
                        <ul class="dropdown-menu">
                            @RenderDropDownItems(item.Items)
                        </ul>
    
                    </li>
                }
                else
                {
                    <li class="nav-item justify-content-end">
                        @* Home *@
                        <a href="@item.Link.Url" class="nav-link py-0 sliding-u-l-r @(Umbraco.AssignedContentItem.Url == item.Link.Url ? "active" : null)">@item.Link.Text</a>
                    </li>
                }   
        }
    }
    }
    
    @helper RenderDropDownItems(List<NavigationListItemModel> listItems)
    {
    foreach (var item in listItems)
    {
        if (item.HasChildren)
        {
            if (@item.Link.Text == "Services")
            { 
                    @item.Link.Url = "/";
             }
    
                <li class="dropdown-submenu">
                    @* Dropdown 1/2/3 *@
                    <a href="@item.Link.Url" class="nav-link py-0 sliding-u-l-r dropdown-toggle @(Umbraco.AssignedContentItem.Url == item.Link.Url ? "active" : null)"
                       data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">@item.Link.Text<b class="caret"></b></a>
                    <ul class="dropdown-menu">
                        @RenderDropDownItems(item.Items)
                    </ul>
                </li>
            }
            else
            {
                @* Action 1 *@
                <li><a href="@item.Link.Url" class="sliding-u-l-r dropdown-item">@item.Link.Text</a></li>
            }
    }
    }
    
  • Alex Skrypnyk 5908 posts 22603 karma points MVP 4x admin c-trib
    Apr 04, 2017 @ 09:43
    Alex Skrypnyk
    100

    Simon,

    I would like to use this code:

    @helper RenderDropDownItems(List<NavigationListItemModel> listItems)
    {
    foreach (var item in listItems)
    {
        if (item.HasChildren)
        {
            var linkUrl = item.Link.Url;
    
            if (item.Link.Text == "Services")
            {
                @linkUrl = "/";
            }
    
                <li class="dropdown-submenu">
                    @* Dropdown 1/2/3 *@
                    <a href="@linkUrl" class="nav-link py-0 sliding-u-l-r dropdown-toggle @(Umbraco.AssignedContentItem.Url == linkUrl ? "active" : null)"
                       data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">@item.Link.Text<b class="caret"></b></a>
                    <ul class="dropdown-menu">
                        @RenderDropDownItems(item.Items)
                    </ul>
                </li>
        }
        else
        {
                @* Action 1 *@
                <li><a href="@item.Link.Url" class="sliding-u-l-r dropdown-item">@item.Link.Text</a></li>
        }
    }
    }
    

    Do not set value to item.Link.Url, just use it and use another variable instead.

    Thanks,

    Alex

  • Simon Cropper 3 posts 85 karma points
    Apr 04, 2017 @ 10:09
    Simon Cropper
    1

    Alex,

    That worked ;-) - though I did have to take the '@' of the assignment statement - so it reads:

    linkUrl = "#";

    Many thanks for your help.

    Simon.

  • Alex Skrypnyk 5908 posts 22603 karma points MVP 4x admin c-trib
    Apr 04, 2017 @ 10:10
    Alex Skrypnyk
    0

    You are welcome, Simon, mark topic as solved and have a great day.

    /Alex

  • 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