问题描述
在一<一href=\"http://stackoverflow.com/questions/14771615/mvc-helpers-working-inline-but-not-in-app-$c$c/14771649#comment20683028_14771649\">$p$pvious帖子我问一个关于入门佣工问题。我成功了,但是当我试图用该技术1.要编写基于Html.RenderAction不同的帮手,和2。要在自己的自定义帮助我得到的错误,一旦他们被出口到APP_ code通过。
In a previous post I asked a question about getting started with helpers. I was successful but when I tried to use the technique 1. To write a different helper based on Html.RenderAction, and 2. To pass in my own custom helper I got errors once they were exported to App_Code.
再次强调的是,他们的工作内联,而不是在出口APP_ code。
Again, to emphasise, they work inline, but not when exported to App_Code.
下面是原来的code:
Here is the original code:
我的许多code的部分只有以下内容:
Many parts of my code have only the following:
<section class="Box">
@{ Html.RenderAction("PageOne"); }
</section>
其他许多地方都有这样的:
Many other parts have this:
@if (@Model.PageTwo)
{
<section class="Box">
@{ Html.RenderAction("PageTwo"); }
</section>
}
所以,我的第一步是把解压出来到内联帮手可能在我所有的code块的使用上面的以下内容:
So my first step was to extract out into an inline helper the following which could be used in all of my code blocks above:
@helper Item(HtmlHelper html, string action, string htmlClass)
{
<section class="@htmlClass">
@{ html.RenderAction(action); }
</section>
}
上面的助手让我来代替,看起来像第一个code段上面这条线的所有code块:
The helper above allows me to replace all the code blocks that look like the first code segment above with this line:
@Item(Html, "PageOne", "Box")
然后我接着写第二个帮手,看起来是这样的:
Then I went on to write the second helper which looks like this:
@helper Item(HtmlHelper html, string action, string htmlClass, bool test)
{
if (test)
{
@Item(html, action, htmlClass)
}
}
这个助手可以让我来代替,看起来像第二code段上面这条线的所有code块:
This helper allows me to replace all the code blocks that look like the second code segment above with this line:
@Item(Html, "PageTwo", "Box", @Model.ShowThisTorF)
我的主要问题是再次,这个工作内联,那么为什么不当我把它消除APP_ code。
My main question once again is, this works inline, so why not when I remove it to App_Code.
有一次,我将它移动到APP_ code我得到以下错误:
第一个问题是关于增加一个使用引用(因为是的HtmlHelper暧昧),而我加code的以下行:
Once I move it to App_Code I get the following errors:The first problem is regarding adding a using reference (because HtmlHelper is ambiguous) to which I add the following line of code:
@using HtmlHelper=System.Web.Mvc.HtmlHelper
这将删除第一个错误,但后来我得到另一个错误:
This removes the first error but then I get another error:
System.Web.Mvc.HtmlHelper不包含一个定义
的RenderAction',没有扩展方法'的RenderAction接受
类型'System.Web.Mvc.HtmlHelper'的第一个参数可以发现(有
你缺少using指令或程序集引用?)
我也试过其他的参考,但没有结果:
I have also tried the other reference but with no result:
@using HtmlHelper=System.Web.WebPages.Html.HtmlHelper
这是我遇到的另一个问题是,我不认为一旦我得到的第一个工作的第二块将工作。尽管它工作得很好直列。
Another problem that I am having is that I don't think the second block will work once I get the first one working. Even though it worked fine inline.
另外,我知道它很明显,但如果我不说这里了,有人会问在他们的答案。当我把它输出到文件APP_ code,根据需要使code的一条线肿块看起来像我确实添加文件名preFIX:
Also, I know its obvious, but if I don't say it here, someone will ask it in their answer. When I moved it out to the file App_Code, I did indeed add the file name prefix as required so the one line lumps of code looked something like:
@Helpers.Item(Html, "PageOne", "Box")
@Helpers.Item(Html, "PageTwo", "Box", @Model.ShowThisTorF)
感谢任何帮助。
推荐答案
正确的HtmlHelper
里面助手在APP_ code目录是 System.Web.Mvc.HtmlHelper
。
The correct HtmlHelper
inside helpers in the App_Code directory is the System.Web.Mvc.HtmlHelper
.
由于的RenderAction
是你需要添加一个扩展方法使用
为在声明这是命名空间 @using System.Web.Mvc.Html
Because RenderAction
is an extension method you need to add a using
for the namespace where it is declared which is @using System.Web.Mvc.Html
所以这个应该工作假设你的文件被命名为 Helpers.cshtml
,并在APP_ $ C $目录C:
So this should work assuming your file is named Helpers.cshtml
and in the App_Code directory:
@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)
这篇关于MVC4 URLHelper工作,但的HtmlHelper不是。我在做什么错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!