asp.net-mvc ASP.NET MVC:Razor 中的自定义 Html 帮助程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4216736/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET MVC: Custom Html Helpers in Razor
提问by awrigley
I am having difficulty with Html Helpers when used with Razor. Said helpers worked fine in MVC 2 with the web form view engine. But not in razor. The error I get at runtime is:
与 Razor 一起使用时,我在使用 Html Helpers 时遇到困难。所述助手在带有 Web 表单视图引擎的 MVC 2 中运行良好。但不是剃须刀。我在运行时得到的错误是:
Compiler Error Message: CS1502: The best overloaded method match for 'System.Web.WebPages.WebPageExecutingBase.Write(System.Web.WebPages.HelperResult)' has some invalid arguments
Source Error:
Line 1: @using Wingspan.Web.Mvc;
Line 2: @Html.IncrementalMenu(MenuBlock.Site)
Expanding the Show Detailed Compiler Output reveals:
展开显示详细的编译器输出显示:
d:\...\Views\Shared\MenuTop.cshtml(2,1): error CS1502: The best overloaded method match for 'System.Web.WebPages.WebPageExecutingBase.Write(System.Web.WebPages.HelperResult)' has some invalid arguments
d:\...\Views\Shared\MenuTop.cshtml(2,7): error CS1503: Argument 1: cannot convert from 'void' to 'System.Web.WebPages.HelperResult'
That indicates to me that razor doesn't like my helper, IncrementalMenu, returning void (which works fine in MVC 2 web form engine views).
这向我表明 razor 不喜欢我的助手 IncrementalMenu 返回 void(在 MVC 2 Web 表单引擎视图中工作正常)。
I get no errors at Compile time, although the line of code (@Html.IncrementalMenu(...)) is red underlined with the following message:
尽管代码行 (@Html.IncrementalMenu(...)) 是红色下划线并带有以下消息,但我在编译时没有收到任何错误:
Cannot implicitly convert type 'void' to 'object'
IncrementalMenu is in the Wingspan.Web.Mvc namespace. It's signature is as follows:
IncrementalMenu 位于 Wingspan.Web.Mvc 命名空间中。它的签名如下:
public static void IncrementalMenu(this HtmlHelper html, MenuBlock menuBlock)
{
// Uses an HtmlTextWriter to render a menu from the sitemap
}
I'm blowed if I know what is wrong...
如果我知道出了什么问题,我会感到震惊......
PS:
PS:
The MenuBlock parameter is just an enum that identifies how the menu should render. Don't fixate on this as that is fine.
MenuBlock 参数只是一个枚举,用于标识菜单应如何呈现。不要专注于此,因为那很好。
回答by GvS
You can call your helper like this:
你可以这样称呼你的助手:
@{ Html.IncrementalMenu(MenuBlock.Site); }
WebForms syntax
WebForms 语法
<% Html.IncrementalMenu(MenuBlock.Site); %>
You just call your method, and the return value (if there is any) is ignored.
您只需调用您的方法,并忽略返回值(如果有)。
Code like this expects a return value, and writes the return value to the html stream:
像这样的代码需要一个返回值,并将返回值写入 html 流:
@Html.YourHelper()
Webforms syntax:
网络表单语法:
<%: Html.YourHelper() %>
The same, if result value != IHtmlString:
相同,如果结果值 != IHtmlString:
<%= Server.HtmlEncode(Html.YourHelper()) %>
回答by awrigley
Addendum:
附录:
You can get the same, or similar, error with @Html.RenderPartial. In this case it is due to the fact that RenderPartial renders directly to the Response, so is not a string and needs to be coded inside a "Razor code block":
您可以使用@Html.RenderPartial 获得相同或类似的错误。在这种情况下,这是由于 RenderPartial 直接呈现给响应,因此不是字符串,需要在“Razor 代码块”中进行编码:
@{
Html.RenderPartial(...);
}
I suspect that is one of the reasons that Microsoft have included in ASP.NET MVC the new Html.Partial. As Html.Partial does return a string, it is OK to write:
我怀疑这是 Microsoft 在 ASP.NET MVC 中包含新的 Html.Partial 的原因之一。由于 Html.Partial 确实返回一个字符串,所以可以这样写:
@Html.Partial
Which looks a lot better. Given that one of Razor's declared objectives is to be easy on the eye, this is quite likely true.
这看起来好多了。鉴于 Razor 宣布的目标之一是让眼睛看起来很舒服,这很可能是真的。
It also kind of makes me, at least, feel more comfortable. I know what returning a string is, I do it all the time. But "returning to the Response" requires a few more brain cycles every time I think it.
至少,这也让我感觉更舒服。我知道返回字符串是什么,我一直这样做。但是每次我想“返回响应”时都需要更多的大脑循环。
And it fits with the old adage that finally Microsoft get their products right in version 3. EG, Access 97.
这符合微软最终在版本 3 中获得正确产品的古老格言。例如,Access 97。
Which is a depressing simile. Cos they screwed things up in version 4, ie, Access 2000...
这是一个令人沮丧的比喻。因为他们在第 4 版中搞砸了,即 Access 2000 ......
回答by Atanas Korchev
Your HTML helper should return MvcHtmlString which represents the html in order to work properly with Razor (and other view engines that are not the WebFormsViewEngine)
您的 HTML 助手应返回表示 html 的 MvcHtmlString 以便与 Razor(以及其他不是 WebFormsViewEngine 的视图引擎)一起正常工作
public static MvcHtmlString Label(this HtmlHelper html, string expression)
{
return MvcHtmlString.Create("<label>" + expression + "</label>");
}