Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用MVC4在局部视图中调用HtmlHelper方法_C#_Asp.net Mvc_Asp.net Mvc 4 - Fatal编程技术网

C# 如何使用MVC4在局部视图中调用HtmlHelper方法

C# 如何使用MVC4在局部视图中调用HtmlHelper方法,c#,asp.net-mvc,asp.net-mvc-4,C#,Asp.net Mvc,Asp.net Mvc 4,我是Mvc4新手。基本上我想创建具有菜单和子菜单的动态选项卡 这是我的Htmlhelper课程: namespace DBMvc.Htmlhlpr { public static class HtmlHelperExtension { public static String ParentMenus(this HtmlHelper html, IEnumerable<menu> Menu) { string htmlOutput = string.Em

我是Mvc4新手。基本上我想创建具有菜单和子菜单的动态选项卡

这是我的Htmlhelper课程:

namespace DBMvc.Htmlhlpr
{
public static class HtmlHelperExtension
{
    public static String ParentMenus(this HtmlHelper html, IEnumerable<menu> Menu)
    {
        string htmlOutput = string.Empty;
        if (Menu.Count() > 0)
        {
            htmlOutput += "<ul class='sf-menu'>";
            var Mainmenu = from mainMenu in Menu where mainMenu.Catid == null select mainMenu;
            foreach (menu m in Mainmenu)
            {
                htmlOutput += "<li>";
                htmlOutput += LinkExtensions.ActionLink(html, m.Depname, null);
                htmlOutput += SubMenus(html, Menu, m.Depid);
                htmlOutput += "</li>";

            }
            htmlOutput += "</ul>";
        }
        return htmlOutput;
    }
    private static string SubMenus(this HtmlHelper html, IEnumerable<menu> SubMenu, int Catid)
    {
        string htmlOutput = string.Empty;
        var subMenu = from sm in SubMenu where sm.Depid ==Catid orderby sm.cats select sm;
        if (subMenu.Count() > 0)
        {
            htmlOutput += "<ul>";
            foreach (menu m in subMenu)
            {
                htmlOutput += "<li>";
                htmlOutput += LinkExtensions.ActionLink(html, m.Depname, null);
                htmlOutput += SubMenus(html, SubMenu, m.Catid);
                htmlOutput += "</li>";
            }
            htmlOutput += "</ul>";
        }
        return htmlOutput;}}}}
在局部视图中,我想使用@Html.Raw调用htmlhelper方法“ParentMenus”。我不知道怎么做。

两件事:

  • 我建议更改HTML帮助程序以返回包含HTML的
    HtmlString
    。然后您可以避免使用
    @Html.Raw()
    ,这几乎总是一种不好的做法(尽管它有其目的)

  • 在Razor视图页面(CSHTML文件)中,使用DBMvc.Htmlhlpr将
    @放在文件顶部,以确保已导入帮助器名称空间。然后使用
    @Html.ParentMenus(…)
    调用帮助程序

  • Item#1严格来说不是必需的,但它是所有内置HTML帮助程序所做的,通常是一种良好的实践

    如果不是这样,请指出您遇到的错误(如果有)。

    方式1: 在视图中包含需要调用的命名空间:

    @using DBMvc.Htmlhlpr
    
    然后使用它:

    @Html.ParentMenus() // pass the parameters which are needed in this IEnumarable<T>
    

    这是一篇解释性文章,这是asp.net mvc的官方文章

    如果我使用HtmlString而不是HtmlHelper,那么在我看来,如果不使用Html.Raw,我该怎么办?直接使用你的助手:
    @Html.ParentMenus(…)
    第一个代码块中的语法不正确。您需要使用
    @使用SomeNamespace
    ,正如我在回答中所示。@Eilon有两种方法,一种是
    {使用DBMvc.Htmlhlpr;}
    @使用DBMvc.Htmlhlpr我不相信这会做同样的事情。我相信这只是一个
    使用
    语句的
    IDisposable
    。在Razor文件中导入名称空间的唯一方法是使用
    @using
    -我刚刚尝试过。使用Foo.Bar执行
    @操作时也没有分号:)
    
    @Html.ParentMenus() // pass the parameters which are needed in this IEnumarable<T>
    
    <system.web.webPages.razor> 
       <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> 
       <pages pageBaseType="System.Web.Mvc.WebViewPage"> 
         <namespaces> 
           <add namespace="System.Web.Mvc" /> 
           <add namespace="System.Web.Mvc.Ajax" /> 
           <add namespace="System.Web.Mvc.Html" /> 
           <add namespace="System.Web.Routing" /> 
           <add namespace="DBMvc.Htmlhlpr" /> 
         </namespaces> 
       </pages> 
    </system.web.webPages.razor> 
    
    @Html.ParentMenus()