开发者

ASP.NET mvc 3 razor : Html Helper Extension Method - executed but dosen't print html

Hi I have problem with my html helper extension method in razor view engine. I want to render SideMenu if I have any nodes to print:

<!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>
</head>
@using MyNamespace.Helpers
<body>
    <div id = "page" style = "width: 90%; margin: 0 auto" class="pageSection">
        <div id="logo" class="pageSection">aaaa</div>
        <div id = "LeftMenuContainer">
        @{ if (ViewBag.Menu.LeftMenu.Count > 0)
           {
               Html.RenderSideMenu((ICollection<MyNamespace.MenuNode>)(ViewBag.Menu.LeftMenu开发者_如何学编程));
           }
         }
         </div>
        <div id="content" class="pageSection">@RenderBody()</div>
        <div id="footer" class="pageSection"></div>
    </div>
</body>
</html>

and here's my method:

public static MvcHtmlString RenderSideMenu(this HtmlHelper helper, ICollection<MenuNode> nodes)
{
    string result = "<h3>Menu</h3>";
    result += "<ul class=\"SideMenu\">";
    foreach (var node in nodes)
        result += MenuRenderEngine.RenderNode(node);
    result += "</ul>";
    return MvcHtmlString.Create(result);
}

The problem is that method is executed and returns well prepared string but doesn't print it in view so I'm confused what did I wrong?


Use @ if you want to output the result on the page:

<div id = "LeftMenuContainer">
    @if (ViewBag.Menu.LeftMenu.Count > 0)
    {
        @Html.RenderSideMenu((ICollection<MyNamespace.MenuNode>)(ViewBag.Menu.LeftMenu));
    }
</div>

Also, as a side note, don't use ViewBag, use view models instead.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜