Bri*_*ins 8 .net asp.net razor asp.net-mvc-3
我通过他的博客看到了Scott Guthrie关于辅助方法的帖子.
特别是这个:

我看到一堆RC版本的MVC 3帖子关于缺少辅助方法...我看到它的语法支持(@helper)得到突出显示,但我有这个/Views/Helpers/SomeHelper.cshtml(定义为局部视图):
@helper SomeHelper(string text)
{
if (text != null)
{
<text>
@text
</text>
}
else
{
<text>
Unknown
</text>
}
}
Run Code Online (Sandbox Code Playgroud)
我用这种方式:
<div>
Helper with Text:
@SomeHelper("This is not null text.")
</div>
Run Code Online (Sandbox Code Playgroud)
但我得到的SomeHelper没有被定义....所以我把它搞砸了?我有什么需要做的事情来将这些视图注册为帮助者吗?
谢谢.
Dre*_*kes 12
我通过App_Code在项目中创建一个Helpers.cshtml文件夹,然后在该文件夹中创建一个文件来完成此操作.
然后,在.cshtml视图中,使用:
@Helpers.SomeHelper("This is not null text.")
Run Code Online (Sandbox Code Playgroud)
这是我发现在整个Web项目中创建共享声明性帮助器方法的唯一方法.如果还有其他人,我想听听他们的意见.
| 归档时间: |
|
| 查看次数: |
1996 次 |
| 最近记录: |