ASP.NET MVC:自定义HTML佣工剃刀 [英] ASP.NET MVC: Custom Html Helpers in Razor

查看:118
本文介绍了ASP.NET MVC:自定义HTML佣工剃刀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

剃刀使用时,我有HTML辅助困难。说佣工MVC 2工作正常的网页表单视图引擎。但不是在剃刀。我在运行时得到的错误是:

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:

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'

这表明,我认为剃刀不喜欢我的帮手,IncrementalMenu,返回void(在MVC 2的Web表单引擎意见工作正常)。

That indicates to me that razor doesn't like my helper, IncrementalMenu, returning void (which works fine in MVC 2 web form engine views).

我得到在编译时没有错误,但code(@ Html.IncrementalMenu(...))的线是红色,下面的消息强调:

I get no errors at Compile time, although the line of code (@Html.IncrementalMenu(...)) is red underlined with the following message:

Cannot implicitly convert type 'void' to 'object'

IncrementalMenu处于Wingspan.Web.Mvc命名空间。它的签名如下:

IncrementalMenu is in the Wingspan.Web.Mvc namespace. It's signature is as follows:

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:

该MenuBlock参数仅标识菜单应如何呈现一个枚举。不要在此注视,因为这是很好。

The MenuBlock parameter is just an enum that identifies how the menu should render. Don't fixate on this as that is fine.

推荐答案

您可以打电话给你的帮助是这样的:

You can call your helper like this:

@{ Html.IncrementalMenu(MenuBlock.Site); }

WebForms的语法

WebForms syntax

<% Html.IncrementalMenu(MenuBlock.Site); %>

您只需拨打你的方法,并返回值(如果有的话)将被忽略。

You just call your method, and the return value (if there is any) is ignored.

code这样需要一个返回值,返回值写入HTML流:

Code like this expects a return value, and writes the return value to the html stream:

@Html.YourHelper()

Web表单语法:

Webforms syntax:

<%: Html.YourHelper() %>

同样的,如果结果值= IHtmlString:

The same, if result value != IHtmlString:

<%= Server.HtmlEncode(Html.YourHelper()) %>

这篇关于ASP.NET MVC:自定义HTML佣工剃刀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆