maartenba / MvcSiteMapProvider

An ASP.NET MVC SiteMapProvider implementation for the ASP.NET MVC framework.
Microsoft Public License
537 stars 220 forks source link

Open link in new tab. #445

Open shhfrlsk06 opened 8 years ago

shhfrlsk06 commented 8 years ago

Can anyone help me on how to configure the sitemapnode to open in new tab, already try using target and target frame but seem not working. already go through your documentation on Defining Sitemap Using xml but not found it there.

Thanks in advance.

NightOwl888 commented 8 years ago

This is what custom attributes are for. You can define a new one named "target" quite easily:

<mvcSiteMapNode title="About" controller="Home" action="About" target="_blank"/>

Then to make sure the new attribute doesn't interfere with routing, you need to add it to AttributesToIgnore.

Internal DI:

<appSettings>
    <add key="MvcSiteMapProvider_AttributesToIgnore" value="target"/>
</appSettings>

External DI (Ninject example):

this.Kernel.Bind<IReservedAttributeNameProvider>().To<ReservedAttributeNameProvider>()
            .WithConstructorArgument("attributesToIgnore", new string[] { "target" });

Then you can access the attribute through the HTML helper templates. Change /Views/Shared/DisplayTemplates/SiteMapNodeModel.cshtml so it can set the new value if it exists (the following assumes you are using at least MVC 4).

@model MvcSiteMapProvider.Web.Html.Models.SiteMapNodeModel
@using System.Web.Mvc.Html
@using MvcSiteMapProvider.Web.Html.Models

@if (Model.IsCurrentNode && Model.SourceMetadata["HtmlHelper"].ToString() != "MvcSiteMapProvider.Web.Html.MenuHelper")  { 
    <text>@Model.Title</text>
}
else if (Model.IsClickable)
{
    var target = Model.Attributes.ContainsKey("target") ? Model.Attributes["target"] : null;
    <a href="@Model.Url" title="@Model.Description" target="@target">@Model.Title</a>
} else {
    <text>@Model.Title</text>
}
shhfrlsk06 commented 8 years ago

ok i understand the concept.. just need help on the HTML helper template, i am using the BootstrapMenuHelperModel Helper, already try but not working,

here is my current HTML helper

    @model MvcSiteMapProvider.Web.Html.Models.MenuHelperModel
    @using System.Web.Mvc.Html
    @using MvcSiteMapProvider.Web.Html.Models

    @helper  TopMenu(List<SiteMapNodeModel> nodeList)
     {
        <nav class="navbar navbar-default" role="navigation">
            <div class="container-fluid">
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle" data-toggle="collapse" data-     target=".navbar-collapse">
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                 </div>
                <div class="collapse navbar-collapse">
                    <ul class="nav navbar-nav">
                        @foreach (SiteMapNodeModel node in nodeList)
                       {
                            string url = node.IsClickable ? node.Url : "#";

                            if (!node.Children.Any())
                            {
                                <li><a href="@url">@node.Title</a></li>
                            }
                            else
                            {
                                <li class="dropdown"><a class="dropdown-toggle" data-    toggle="dropdown">@node.Title <span class="caret"></span>    </a>@DropDownMenu(node.Children)</li>
                        }

                            if (node != nodeList.Last())
                            {
                                <li class="divider-vertical"></li>
                            }
                        }
                    </ul>
                    @Html.Partial("_LoginPartial")
                </div>
            </div>
        </nav>
    }

    @helper DropDownMenu(SiteMapNodeModelList nodeList)
    {
        <ul class="dropdown-menu" role="menu">
            @foreach (SiteMapNodeModel node in nodeList)
                {
                    if (node.Title == "Separator")
                {
                    <li class="divider"></li>
                    continue;
                }

                string url = node.IsClickable ? node.Url : "#";

                if (!node.Children.Any())
                {
                    <li><a href="@url">@node.Title</a></li>
                }
                else
                {
                    <li class="dropdown-submenu"><a href="@url">@node.Title</a>@DropDownMenu(node.Children)</li>
                }
            }
        </ul>
    }

    @TopMenu(Model.Nodes)
bjamesbc commented 7 years ago

That was perfect!! Thanks!!