Solved

.NET MVC 3 & Web.sitemap

Posted on 2011-03-10
4
3,759 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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.NET 2008 Winforms Signing 13 42
SSIS Start 2 45
VB.Net creating Contact in Outlook 1 59
Error building VS2105 solution from repository 1 39
It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

730 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