Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Razor void function

I have Razor function which outputs some data and as result does not return anything (that's a long story why it is done this way):

@functions
{
  public static void SampleHelperMethod()
  {
    //...
  }
}

How can I call it in view now? I tried @MyFunctions.SampleHelperMethod() but it doesn't work for void functions.

like image 798
SiberianGuy Avatar asked Feb 20 '23 21:02

SiberianGuy


1 Answers

Declaration

@functions
{
    public static void TestFunction()
    {

    }
}

Use in View

@{ TestFunction(); }

Because this is a function that does not return anything, you need to wrap it in the braces like you would and if/for statement. However, like Erik said, it is really unclear why this logic would be declared in the view...you may consider creating a helpers class that your views can include. This will allow for reuse and better separations of concerns.

like image 99
Tommy Avatar answered Feb 28 '23 03:02

Tommy