ASp.NET MVC 3.0 调用通用扩展 html 帮助器方法时出错
我有一个这样的扩展方法:
namespace System.Web.Mvc.Html
{
public static class HtmlExtensions
{
public static T GetEnumValue<T>(this HtmlHelper helper, int value) where T : struct, IConvertible
{
return EnumHelper<T>.GetEnumValue(value);
}
}
}
然后我在 Razor 视图上调用此方法(自动完成该方法的工作,它在视图上可见),但我收到了错误:
@Html.GetEnumValue<MyEnumHere>(1) //Getting error here
错误: 无法将方法组“GetEnumValue”转换为非委托类型“object”。您打算调用该方法吗?
如果我这样做 - 编译时没有错误:
Html.GetEnumValue<MyEnumHere>(1) //but in that case didnt get data to display.
如果出现该错误,则在编译时也不会出现错误
@{
Html.GetEnumValue<MyEnum>(1); //But then I am getting error during execution
}
:方法“Write”没有重载需要 0 个参数 有什么
建议吗?
更新0.1:
让它像这样工作:
var value = Html.GetEnumValue<MyEnum>(1);
@value
仍然质疑为什么在这种情况下它不起作用:
@Html.GetEnumValue<MyEnumHere>(1)
更新0.2
在我更新我的扩展方法以返回IHtmlStirng之后
仍然无法正常工作:
@using MyTypes.Enumerators
@inherits MvcContrib.FluentHtml.ModelWebViewPage<MyModel>
@foreach (var thing in Model.Stuff)
{
@Html.GetEnumValue<MyEnum>(thing.Id)
}
执行期间出错:
'foreach 块缺少结束“}”字符。确保此块中的所有“{”字符都有匹配的“}”字符,并且没有任何“}”字符被解释为标记。
由于某种原因解释为 html 标签(收到警告:警告 1 “MyEnum”元素未关闭。所有元素必须是自关闭的或具有匹配的结尾标记。),在这种情况下我也无法导航到我的扩展方法,但是如果我从声明中删除 @
(Html.GetEnumValue
) 比我可以导航我的方法
I have an extension method like that:
namespace System.Web.Mvc.Html
{
public static class HtmlExtensions
{
public static T GetEnumValue<T>(this HtmlHelper helper, int value) where T : struct, IConvertible
{
return EnumHelper<T>.GetEnumValue(value);
}
}
}
Then I am calling this method on the Razor View(auto complete for that method working, it is visible on the view), but than i am getting error:
@Html.GetEnumValue<MyEnumHere>(1) //Getting error here
error : Cannot convert method group 'GetEnumValue' to non-delegate type 'object'. Did you intend to invoke the method?
If i go like that - no errors during compile time:
Html.GetEnumValue<MyEnumHere>(1) //but in that case didnt get data to display.
Also not getting errors during compile time if go lie that
@{
Html.GetEnumValue<MyEnum>(1); //But then I am getting error during execution
}
error: No overload for method 'Write' takes 0 arguments
Any suggestions?
Update 0.1:
Get it work like that:
var value = Html.GetEnumValue<MyEnum>(1);
@value
still question why in that case it is doesn't work:
@Html.GetEnumValue<MyEnumHere>(1)
Update 0.2
after i updated my extension method to return IHtmlStirng
still didn't get it work:
@using MyTypes.Enumerators
@inherits MvcContrib.FluentHtml.ModelWebViewPage<MyModel>
@foreach (var thing in Model.Stuff)
{
@Html.GetEnumValue<MyEnum>(thing.Id)
}
error during execution :
'The foreach block is missing a closing "}" character. Make sure you have a matching "}" character for all the "{" characters within this block, and that none of the "}" characters are being interpreted as markup.'
<MyEnum>
for some reason interpreting as a html tag(getting warning: Warning 1 The "MyEnum" element was not closed. All elements must be either self-closing or have a matching end tag.) and also i can't navigate to my extension method in that case, but if i remove @
from declaration (Html.GetEnumValue<MyEnum>(thing.Id)
) than i can navigate my method
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通常 HTML 助手应该返回字符串或 IHtmlString 因为这就是它们用于(生成您在视图中重复使用的简短 HTML 片段)。
所以也许您想要这样:
然后在您看来,您将能够像这样调用它(请注意,如果您想将泛型用作
<
和>
被 Razor 解析器视为特殊字符):Normally HTML helpers should return strings or IHtmlString because that's what they are used for (generate short HTML snippets that you reuse in your views).
So maybe you want this:
and then in your view you will be able to invoke it like this (note that you might need to wrap it in parenthesis if you want to use generics as
<
and>
are treated like special characters by the Razor parser):