Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.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 URLHelper正在工作,但HTMLHelper不工作。我做错了什么_C#_Asp.net Mvc_Asp.net Mvc 4 - Fatal编程技术网

C# MVC4 URLHelper正在工作,但HTMLHelper不工作。我做错了什么

C# MVC4 URLHelper正在工作,但HTMLHelper不工作。我做错了什么,c#,asp.net-mvc,asp.net-mvc-4,C#,Asp.net Mvc,Asp.net Mvc 4,在一次访谈中,我问了一个关于如何开始帮助的问题。我是成功的,但当我尝试使用技巧1。根据Html.RenderAction和2编写不同的帮助程序。为了传递我自己的自定义助手,在将它们导出到App_代码后,我得到了错误 再次强调的是,它们可以内联工作,但在导出到App_代码时不能工作 以下是原始代码: 我的代码的许多部分只有以下内容: <section class="Box"> @{ Html.RenderAction("PageOne"); } <

在一次访谈中,我问了一个关于如何开始帮助的问题。我是成功的,但当我尝试使用技巧1。根据Html.RenderAction和2编写不同的帮助程序。为了传递我自己的自定义助手,在将它们导出到App_代码后,我得到了错误

再次强调的是,它们可以内联工作,但在导出到App_代码时不能工作

以下是原始代码:

我的代码的许多部分只有以下内容:

    <section class="Box">
        @{ Html.RenderAction("PageOne"); }
    </section>
然后我继续写第二个助手,看起来像这样:

@helper Item(HtmlHelper html, string action, string htmlClass, bool test)
{
    if (test)
    {
        @Item(html, action, htmlClass)
    }
}
此帮助程序允许我用以下行替换所有看起来像上面第二个代码段的代码块:

@Item(Html, "PageOne", "Box")
@Item(Html, "PageTwo", "Box", @Model.ShowThisTorF)
我的主要问题再次是,这是内联工作的,所以当我将其删除到App_代码时为什么不呢

一旦我将其移动到App_代码,就会出现以下错误: 第一个问题是关于添加using引用,因为HtmlHelper不明确,我添加了以下代码行:

 @using HtmlHelper=System.Web.Mvc.HtmlHelper
这会删除第一个错误,但随后会出现另一个错误:

System.Web.Mvc.HtmlHelper不包含的定义 “RenderAction”和无扩展方法“RenderAction”接受 可以找到类型为“System.Web.Mvc.HtmlHelper”的第一个参数是 是否缺少using指令或程序集引用

我也尝试过其他参考,但没有结果:

 @using HtmlHelper=System.Web.WebPages.Html.HtmlHelper
我面临的另一个问题是,我认为一旦我让第一个模块工作起来,第二个模块就不工作了。尽管它在内部运行良好

而且,我知道这很明显,但是如果我不在这里说,有人会在他们的回答中问这个问题。当我把它移到文件App_代码中时,我确实根据需要添加了文件名前缀,因此一行代码看起来像:

@Helpers.Item(Html, "PageOne", "Box")
@Helpers.Item(Html, "PageTwo", "Box", @Model.ShowThisTorF)
感谢您的帮助。

应用程序代码目录中正确的HtmlHelper内部帮助程序是System.Web.Mvc.HtmlHelper

因为RenderAction是一种扩展方法,所以需要为声明它的名称空间添加using,即@using System.Web.Mvc.Html

因此,假设您的文件名为Helpers.cshtml,并且位于App_Code目录中,则此项功能应该可以正常工作:

@using HtmlHelper=System.Web.Mvc.HtmlHelper
@using System.Web.Mvc.Html

@helper Item(HtmlHelper html, string action, string htmlClass)
{
    <section class="@htmlClass">
        @{ html.RenderAction(action); }
    </section>
}
@helper Item(HtmlHelper html, string action, string htmlClass, bool test)
{
    if (test)
    {
        @Item(html, action, htmlClass)
    }
}
App_代码目录中正确的HtmlHelper内部帮助程序是System.Web.Mvc.HtmlHelper

因为RenderAction是一种扩展方法,所以需要为声明它的名称空间添加using,即@using System.Web.Mvc.Html

因此,假设您的文件名为Helpers.cshtml,并且位于App_Code目录中,则此项功能应该可以正常工作:

@using HtmlHelper=System.Web.Mvc.HtmlHelper
@using System.Web.Mvc.Html

@helper Item(HtmlHelper html, string action, string htmlClass)
{
    <section class="@htmlClass">
        @{ html.RenderAction(action); }
    </section>
}
@helper Item(HtmlHelper html, string action, string htmlClass, bool test)
{
    if (test)
    {
        @Item(html, action, htmlClass)
    }
}

谢谢你的回答。我教过它可能只是一些小的改变。非常感谢。谢谢你的回答。我教过它可能只是一些小的改变。非常感谢。
@using HtmlHelper=System.Web.Mvc.HtmlHelper
@using System.Web.Mvc.Html

@helper Item(HtmlHelper html, string action, string htmlClass)
{
    <section class="@htmlClass">
        @{ html.RenderAction(action); }
    </section>
}
@helper Item(HtmlHelper html, string action, string htmlClass, bool test)
{
    if (test)
    {
        @Item(html, action, htmlClass)
    }
}
@Helpers.Item(Html, "PageOne", "Box")
@Helpers.Item(Html, "PageTwo", "Box", @Model.ShowThisTorF)