声明辅助方法在剃刀RTM [英] Declarative Helper Methods in Razor RTM

查看:137
本文介绍了声明辅助方法在剃刀RTM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到斯科特Guthrie的职位有关的辅助方法,通过他的博客

I saw Scott Guthrie's post about helper methods via his blog.

具体是这样:

我看到了一堆RC版对缺乏辅助方法MVC 3的职位......我看它的语法支持( @helper )被突出显示,但我有这个在 /Views/Helpers/SomeHelper.cshtml (定义为局部视图):

I see the bunch of RC version of MVC 3 posts about the lack of helper methods... I see the syntactical support for it (@helper) gets highlighted, but I have this in /Views/Helpers/SomeHelper.cshtml (defined as a partial view):

@helper SomeHelper(string text)
{
    if (text != null)
    {
        <text>
            @text
        </text>
    }
    else
    {
        <text>
            Unknown
        </text> 
    }
}

我用这种方式:

I use it this way:

<div>
Helper with Text:
@SomeHelper("This is not null text.")
</div>

不过,我得到SomeHelper没有定义....所以在这里做了我搞砸了?有什么我需要做的,注册这些意见作为帮手?

But I get SomeHelper is not defined.... so where did I mess this up? Is there something I need to do to register these views as helpers?

感谢。

推荐答案

我已经在我的项目创建一个 APP_ code 文件夹,然后创建做到了这一点一个 Helpers.cshtml 该文件夹中的文件。

I've done this by creating an App_Code folder in my project, then creating a Helpers.cshtml file in that folder.

然后,在 .cshtml 视图,使用:

@Helpers.SomeHelper("This is not null text.")

这是我发现创建整个Web项目的共享声明辅助方法的唯一途径。如果有其他人,我想听听他们。

This is the only way I've found to create shared declarative helper methods across the entire web project. If there are others, I'd like to hear about them.

这篇关于声明辅助方法在剃刀RTM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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