简体   繁体   English

.NET MVC自定义助手在布局中无法正确呈现

[英].NET MVC Custom helper not rendering properly in layout

I've read all the walkthroughs and examples I can find, and I can't figure out what's missing. 我已经阅读了所有可以找到的演练和示例,但无法弄清缺少的内容。

My helper... 我的助手

namespace MVCShop.Helpers
{
    public class RenderNav
    {
        public static MvcHtmlString GetCategoryNav(string store)
        {
            MVCShopEntities db = new MVCShopEntities();

            IEnumerable<Category> categories = db.Categories.Where(c => c.Store.Name == store);
            StringBuilder sb = new StringBuilder();

            foreach (Category cat in categories)
            {
                sb.AppendFormat("<ul id='menu'><li>{0}</li></ul>", cat.Name);
            }

            return new MvcHtmlString(sb.ToString());
        }
    }
}

In my _Layout, I've added this line... 在我的_Layout中,我添加了这一行...

    <nav>
            @{ RenderNav.GetCategoryNav(ViewContext.RouteData.Values["storeName"].ToString()); }
    </nav>

I've stepped through the code. 我已经完成了代码。 The string is being properly created and returned by the function. 该字符串已正确创建并由函数返回。 Everything builds, and there are no runtime errors. 一切都建立了,没有运行时错误。 The menu html is simply not displaying on the page. 菜单html根本不在页面上显示。 I'm stumped. 我很沮丧

You're not writing the value, you're just calling the method. 您不是在值,而是在调用方法。
The value is generated and then gracefully ignored . 生成该值,然后正常忽略

Remove the brackets (which are used for code blocks and don't output any content) and the semicolon: 除去括号(用于代码块,不输出任何内容)和分号:

<nav>
    @RenderNav.GetCategoryNav(ViewContext.RouteData.Values["storeName"].ToString())
</nav>

This will tell Razor to output method result and display your navigation. 这将告诉Razor 输出方法结果并显示您的导航。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM