I have functions in my view that is shared by several pages:
@functions
{
public HtmlString ModeImage(ModeEnum mode)
{
switch(mode)
{
(Here's a more detailed version of the existing answers.)
Create a folder called App_Code
in the root of the MVC project if it doesn't already exist. In here, create an empty razor view and name it whatever you want:
Add @helper
s and/or static
methods to it as needed:
@helper ShowSomething()
{
Something
}
@functions
{
public static int CalculateSomething()
{
return 1;
}
}
Then use them from your views by first accessing the shared view by name:
@Shared.ShowSomething()
@Shared.CalculateSomething()