TrN*_*TrN 2 razor asp.net-mvc-3
嗨,我在剃刀视图引擎中的html帮助扩展方法有问题.如果我要打印任何节点,我想渲染SideMenu:
<!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>
Run Code Online (Sandbox Code Playgroud)
这是我的方法:
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);
}
Run Code Online (Sandbox Code Playgroud)
问题是方法执行并返回准备好的字符串,但不会在视图中打印出来,所以我很困惑我错了什么?
使用@如果要输出页面上的结果:
<div id = "LeftMenuContainer">
@if (ViewBag.Menu.LeftMenu.Count > 0)
{
@Html.RenderSideMenu((ICollection<MyNamespace.MenuNode>)(ViewBag.Menu.LeftMenu));
}
</div>
Run Code Online (Sandbox Code Playgroud)
另外,作为旁注,请勿使用ViewBag,请改用视图模型.
| 归档时间: |
|
| 查看次数: |
2619 次 |
| 最近记录: |