Solved

.NET MVC 3 & Web.sitemap

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

770 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