Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

.NET MVC 3 & Web.sitemap

Posted on 2011-03-10
4
Medium Priority
?
3,778 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
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 course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…

609 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