Solved

.NET MVC 3 & Web.sitemap

Posted on 2011-03-10
4
3,744 Views
Last Modified: 2013-11-08
I'm new to MVC and am trying to use a Web.sitemap but the HTML is displaying like it's encoded instead of displaying the HTML output.

Here's the MenuHelper.cs code:

using System.Text;
using System.Web;
using System.Web.Mvc;

namespace MvcBrands.Helpers
{
    public static class MenuHelper
    {
        public static string Menu(this HtmlHelper helper)
        {
            var sb = new StringBuilder();

            // Create opening unordered list tag
            sb.Append("<ul class='menu'>");

            // Render each top level node
            var topLevelNodes = SiteMap.RootNode.ChildNodes;
            foreach (SiteMapNode node in topLevelNodes)
            {
                if (SiteMap.CurrentNode == node) sb.AppendLine("<li class='selectedMenuItem'>");
                else sb.AppendLine("<li>");

                sb.AppendFormat("<a href='{0}'>{1}</a>", node.Url, node.Title);
                sb.AppendLine("</li>");
            }

            sb.AppendLine("</ul>");

            return sb.ToString();
        }
    }
}

Here's the Web.sitemap data:

<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
    <siteMapNode url="" title=""  description="">
        <siteMapNode url="" title="Products"  description="" />
        <siteMapNode url="" title="Gallery"  description="" />
        <siteMapNode url="" title="Markets"  description="" />
        <siteMapNode url="" title="Custom"  description="" />
        <siteMapNode url="" title="Downloads"  description="" />
        <siteMapNode url="" title="How To Buy"  description="" />
        <siteMapNode url="" title="About Us"  description="" />
    </siteMapNode>
</siteMap>

And here's the _Layout.cshtml file code:

@using MvcBrands.Helpers;
<!DOCTYPE html>
<html>
<head>
    <title>@ViewBag.Title</title>
    <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
    <script src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")" type="text/javascript"></script>
    <style type="text/css">
        .menu
        {
            list-style: none;
            padding: 0px;
            margin: 0px;
        }
       
        .menu li
        {
            float: left;
        }
       
        .menu a
        {
            display: block;
            background-color: #eeeeee;
            color: Black;
            font-weight: bold;
            padding: 4px;
            border: solid 1px black;
            text-decoration: none;
            margin: 2px;
        }
       
        .selectedMenuItem a
        {
            background-color: White;
        }
    </style>
</head>
<body>
    <div>
        @Html.Menu()
    </div>
    @RenderBody()
</body>
</html>

See attached image for the output.

HTML Output
0
Comment
Question by:infotechelg
  • 2
4 Comments
 
LVL 10

Expert Comment

by:pramodsk40
ID: 35109751
Try using....within <%:  %>.

Also read this to understand more clearly

http://stevesmithblog.com/blog/default-encoding-of-strings-in-asp-net-mvc-2/
0
 

Author Comment

by:infotechelg
ID: 35111594
Thanks pramodsk40. If you'll notice, I'm not using <% anywhere. So there's not <% to replace with <%:

0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 35136405
In order for your helper to play nice with Razor you need to return a MvcHtmlString rather than a plain string:
namespace MvcBrands.Helpers
{
    public static class MenuHelper
    {
        public static MvcHtmlString Menu(this HtmlHelper helper)
        {
            var sb = new StringBuilder();

            // Create opening unordered list tag
            sb.Append("<ul class='menu'>");

            // Render each top level node
            var topLevelNodes = SiteMap.RootNode.ChildNodes;
            foreach (SiteMapNode node in topLevelNodes)
            {
                if (SiteMap.CurrentNode == node) sb.AppendLine("<li class='selectedMenuItem'>");
                else sb.AppendLine("<li>");

                sb.AppendFormat("<a href='{0}'>{1}</a>", node.Url, node.Title);
                sb.AppendLine("</li>");
            }

            sb.AppendLine("</ul>");

            return MvcHtmlString.Create(sb.ToString());
        }
    }
}

Open in new window

0
 

Author Closing Comment

by:infotechelg
ID: 35141961
Brilliant! Thanks, Carl.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now