4

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

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

How Can I resolve this please ?

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

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>
}
sacretruth
  • 780
  • 2
  • 18
  • 34

4 Answers4

10

Restart Visual Studio

Clean and rebuild alone was not enough but the steps that worked for me were:

  1. Clean solution
  2. Restart Visual Studio (2012)
  3. Rebuild solution

After those steps, the Visual Studio Intellisense picked it up again.

Rob
  • 171
  • 2
  • 2
6

Try to build/rebuild the project (if your helper is in the App_Code folder).

Then VS will recognize the helper.

Safeer Hussain
  • 1,230
  • 1
  • 15
  • 27
5

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

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):

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.

archil
  • 39,013
  • 7
  • 65
  • 82
  • VisualStudio seems to create some 'gobbledygook' class name that seems to look like my helper, but this still doesn't allow me access to any of the helper methods. – sacretruth Sep 26 '12 at 12:12
  • You should show the code then, because way described in my answer works – archil Sep 26 '12 at 12:28
  • You cannot call your helper like @Html.TextBox as that is razor helper, not html helper. You should call it as Darin Dimitrov suggested – archil Sep 26 '12 at 13:01
  • 1
    If it works in one environment and yells in another (i.e. production server), try this: http://stackoverflow.com/a/13597285/484108 – Phillippe Santana Sep 19 '16 at 17:12
1

In any view you could call your custom Razor helper like this:

@Helpers.TextBox("some title", "someid", "default value", false)

This assumes that your helper is defined inside ~/App_Code/Helpers.cshtml.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • To be honest, I did this exactly before coming on stackoverflow. It actually works when I run the page, however, visual studio doesn't seem to recognise the helper. It comes out with a squiggly line underneath the call – sacretruth Sep 26 '12 at 13:40
  • Oh, you should have mentioned that. Razor Intellisense is far from perfect in Visual Studio. We can hope that they will improve it in future versions. – Darin Dimitrov Sep 26 '12 at 14:03