剃刀无效功能 [英] Razor void function

查看:73
本文介绍了剃刀无效功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Razor函数,可以输出一些数据,结果不返回任何东西(这是一个很长的故事,为什么这样做):

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()
  {
    //...
  }
}

现在如何在视图中调用它?我尝试了 @ MyFunctions.SampleHelperMethod(),但不适用于void函数.

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

推荐答案

声明

@functions
{
    public static void TestFunction()
    {

    }
}

在视图中使用

@{ TestFunction(); }

因为这是一个不返回任何内容的函数,所以您需要像将其和if/for语句一样将其包装在花括号中.但是,就像Erik所说的那样,真的不清楚为什么要在视图中声明此逻辑……您可以考虑创建一个可以包含您的视图的助手类.这将允许重用和更好地分离关注点.

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.

这篇关于剃刀无效功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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