7

In my App_code, I have a helper function called FormatTelephone(string number) in Formatter.cshtml. I tried to access it in a partial view by @Formatter.FormatTelephone(number). When I test it, it says

Compiler Error Message: CS0103: The name 'Formatter' does not exist in the current context

What is the likely cause of it? Thanks!

uni
  • 613
  • 1
  • 7
  • 11

3 Answers3

7

I ran into this exact problem when deploying the site onto another server. Make sure the App_Code/Formatter.cshtml file is actually copied to the server! My mistake was the file has a build action that was set to 'None'. Right click on the file and select Properties, and then set the Build Action to 'Content'.

NOTE:

If you don't have asp.net mvc 3/4 installed make sure the following dlls are in your bin folder.

  • System.Web.Mvc
  • Microsoft.Web.Infrastructure
  • System.Web.Razor
  • System.Web.WebPages
  • System.Web.WebPages.Razor

Scott Hanselman has a blog post about what might be causing your issue. BIN Deploying ASP.NET MVC 3 with Razor to a Windows Server without MVC installed

superlogical
  • 14,332
  • 9
  • 66
  • 76
  • I ran into the same situation. The helpers didn't work in the deployed server and the problem is that by default the helper files has the build action set to 'None'. Thank you for the tip! – vcRobe Nov 01 '17 at 13:35
0

I ran into a similar problem. My issue was that I was using razor generator and the name.generated.cs file "Build Action" property was "None" and it needs to be "Compile". The "Build Action" property or the name.cs file was "Content" as per the answer and this explains why the cshtml files calling the method could "see" the method, intellisense worked, I could jump into the definition of the method from the cshtml file. The generated.cshtml file though did not compile.

Change the "Build Action" to "Compile". I am surprised that it was not this already TBH.

I hope this saves someone an hour of mindless repetition.

0

The following works for me in ~/App_Code/Formatter.cshtml:

@helper FormatTelephone(string number)
{
    <div>Formatted @number</div>
}

and then in some view:

@Formatter.FormatTelephone("123")
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 1
    Yes. I want to share this helper function in multiple views, so I put it in `App_code\Formatter.cshtml`. I'm trying to access it in `Views\Shared\_InfoView.cshtml`. However, it doesn't work and says `Formatter` doesn't exist. It's weird, because when I type it, intellisense recognized `Formatter`. – uni Oct 20 '11 at 17:03
  • @uni, what parameter are you passing? It must be a string. Try hardcoding a value as I did in my example. – Darin Dimitrov Oct 20 '11 at 17:04
  • Yes, I'm sure it is a string. – uni Oct 20 '11 at 17:05
  • I just tried and it didn't work. Actually, it says `Formatter` cannot be found, not the function. – uni Oct 20 '11 at 17:07
  • Is there anything to do with Build Action of Formatter.cshtml? – uni Oct 20 '11 at 17:09
  • Hmm very weird, works for me. There is nothing to do with Build Action. Is `Formatter.cshtml` the only file you have in `App_Code`? Also could you confirm that you used `@helper` and not `@function`? Also did you try calling it from a standard view and not a partial? – Darin Dimitrov Oct 20 '11 at 17:10
  • It's the only file in App_Code, and I'm using @helper. Actually, they all worked before, until I refactor it into App_Code. – uni Oct 20 '11 at 17:12
  • @uni, sorry I have no more clues as I am unable to reproduce the issue. – Darin Dimitrov Oct 20 '11 at 17:14