SiteMapPath as an unordered list

May 13, 2010 at 1:05 AM

Hello all,

I'm not very familiar with writing or adapting helpers, so my question is: has anyone of you ever made a LIST-version of the SiteMapPath-htmlhelper and if so, are you willing to share the code? I did a search but I can't find it anywhere on the web.

Greetings,

 

Salvi

 

May 13, 2010 at 10:43 AM

Hi,

This is what I use to create a list...

<%=Html.Menu("MvcSiteMap", true, false, "active", 99, new { id = "nav" })%>

Hope it helps.  I am not that clued up with all this but it seems to work for me.

Thanks,

Chris

 

 

May 13, 2010 at 2:22 PM

Hi Chris,

Thank you for your reply.

I am looking for a way to turn the SiteMapPath into an unordered list, as in the breadcrumbs option, not the menu.

These days also breadcrumbs tend to be coded using an unordered list rather than a sequence of links with separators in between.

 

Salvi

 

May 13, 2010 at 5:47 PM
Edited May 13, 2010 at 6:04 PM

(Codeplex does not like Firefox) (look at next post)

May 13, 2010 at 5:55 PM
Edited May 13, 2010 at 6:04 PM

(Codeplex does not like Firefox) (look at next post)

May 13, 2010 at 6:03 PM
Edited May 13, 2010 at 6:11 PM

So let's try it in Internet Explorer (jeez!):

I made a BreadCrumbsHelper as an alternative to SiteMapPathHelper. It works with only one ID to specify, I kicked everything else out of the code to get the html as clean as possible.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace MvcSiteMap.Core.Helpers
{
    /// <summary>
    /// HtmlHelper extension methods
    /// </summary>
    public static class BreadCrumbsHelper
    {
        /// <summary>
        /// Gets BreadCrumbs for the current request
        /// </summary>
        /// <param name="helper">HtmlHelper instance</param>
        /// <returns>BreadCrumbs path for the current request</returns>
        public static string BreadCrumbs (this HtmlHelper helper)
        {
            return BreadCrumbs(helper, "crumbs", false);
        }

        /// <summary>
        /// Gets BreadCrumbs for the current request
        /// </summary>
        /// <param name="helper">HtmlHelper instance</param>
        /// <param name="listId">Id of the unordered list</param>
        /// <returns>BreadCrumbs path for the current request</returns>
        public static string BreadCrumbs (this HtmlHelper helper, string listId)
        {
            return BreadCrumbs(helper, listId, false);
        }

        /// <summary>
        /// Gets BreadCrumbs path for the current request
        /// </summary>
        /// <param name="helper">HtmlHelper instance</param>
        /// <param name="listId">Id of the unordered list</param>
        /// <param name="currentNodeAsLink">Can the current SiteMap node be displayed as a link?</param>
        /// <returns>BreadCrumbs path for the current request</returns>
        public static string BreadCrumbs(this HtmlHelper helper, string listId, bool currentNodeAsLink)
        {
            List<string> pages = new List<string>();

            SiteMapNode node = SiteMap.CurrentNode;
            while (node != null)
            {
                if (node.IsAccessibleToUser(HttpContext.Current))
                {
                    pages.Add(BreadCrumbsLink(helper, node, currentNodeAsLink));
                }
                node = node.ParentNode;
            }
            pages.Reverse();
            
            // Turn pages into a nice little unordered list
            var builder = new StringBuilder();
            builder.AppendFormat("\n<ul id=\"{0}\">", listId);
            foreach (string page in pages)
            {
                builder.AppendFormat("\n\t<li>{0}</li>", page.ToString());
            }
            builder.Append("\n</ul>");

            return builder.ToString();
        }

        /// <summary>
        /// Gets a BreadCrumbs link
        /// </summary>
        /// <param name="helper">HtmlHelper instance</param>
        /// <param name="node">The node to get a BreadCrumbs link for</param>
        /// <param name="currentNodeAsLink">Can the current BreadCrumbs node be displayed as a link?</param>
        /// <returns>BreadCrumbs link for the specified node</returns>
        private static string BreadCrumbsLink(this HtmlHelper helper, SiteMapNode node, bool currentNodeAsLink)
        {

            if (currentNodeAsLink || !node.Equals(SiteMap.CurrentNode))
            {

                // Determine extra attributes
                StringBuilder extraAttributes = new StringBuilder();
                MvcSiteMapNode mvcNode = node as MvcSiteMapNode;
                if (mvcNode != null)
                {
                    if (!string.IsNullOrEmpty(mvcNode.Target))
                    {
                        extraAttributes.Append(" target=\"" + mvcNode.Target + "\"");
                    }
                    if (!string.IsNullOrEmpty(mvcNode.Title))
                    {
                        extraAttributes.Append(" title=\"" + mvcNode.Title + "\"");
                    }
                }

                return string.Format("<a href=\"{0}\"{1}>{2}</a>", node.Url, extraAttributes, helper.Encode(node.Title));
            }
            else
            {
                return helper.Encode(node.Title);
            }
        }

    }
}

A few ways to call it (default ul id is "crumbs"):

            <%=Html.BreadCrumbs()%>
            <%=Html.BreadCrumbs("myBreadCrumbsUl")%>
            <%=Html.BreadCrumbs("myBreadCrumbsUl", true)%>

 

The first one would output something like this in html, rather than a continuous string:

<ul id="crumbs">
	<li><a href="/" title="Home">Home</a></li>
	<li><a href="/Account" title="Account">Account</a></li>
	<li>Log in</li>
</ul>