Solved

.NET MVC 3 & Web.sitemap

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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 video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

760 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

17 Experts available now in Live!

Get 1:1 Help Now