当前上下文中不存在MVC Razor帮助器 [英] MVC Razor helper does not exist in current context

查看:42
本文介绍了当前上下文中不存在MVC Razor帮助器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在我的MVC4 Web应用程序中创建了一个自定义剃刀助手,我需要在所有视图中都可以使用它.

I have created a custom razor helper in my MVC4 Web application that I need to be usable in all of my views.

在我所有的查看页面中,我似乎都无法使用自定义助手.VS2012不仅仅是看到它.

In all of my view pages, I can't seem to use my custom helper. VS2012 doesn't just see it.

请问该如何解决?

编辑:当我运行页面时,它实际上可以工作,只是VS没有看到它.

EDIT: It actually works when I run the page, it's just VS that doesn't see it.

这是我的助手,位于我的AppCode文件夹中的Helpers.cshtml中.

Here is my helper which is located in Helpers.cshtml within my AppCode folder.

@helper TextBox(string title, string id, string placeholder, bool required){    
<ul>
    <li>
        <label for="@id">@title</label>
    </li>
    <li>
        <input type="text" name="this" class="@if (@required) {<text>required</text>}" minlength="2" id="@id" placeholder="@placeholder" />
    </li>
</ul>
}

推荐答案

如果是

If it is razor helper(using @helper syntax), you should define it in view placed within \App_Code

我们可以通过将@helper方法保存在放置在\ App_Code目录中的.cshtml/.vbhtml文件您在项目的根目录中创建的内容.例如,下面我在\ App_Code文件夹中创建了一个"ScottGu.cshtml"文件,然后在文件中定义了两个单独的帮助程序方法(您可以有任何每个文件中辅助方法的数量):

We can accomplish this by saving our @helper methods within .cshtml/.vbhtml files that are placed within a \App_Code directory that you create at the root of a project. For example, below I created a "ScottGu.cshtml" file within the \App_Code folder, and defined two separate helper methods within the file (you can have any number of helper methods within each file):

如果它是更传统的html助手,则应通过将记录添加到在以下代码中定义的< system.web.webPages.razor> namespaces 元素中来引用它〜\ Views \ Web.Config .如果只想在单一视图中使用它,则可以在视图顶部添加 @using 指令.

And if it is more traditional html helper, you should reference it, by adding record to namespaces element of <system.web.webPages.razor> defined in ~\Views\Web.Config. If you want to use it only in singe view, you could add @using directive on top of view.

这篇关于当前上下文中不存在MVC Razor帮助器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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