开发者

.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 _Layo开发者_如何转开发ut, I've added this line...

    <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. 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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜