mvc的help和functions语法

@helper show(int num ) 
{ 
    if (num == 0)
    {
        @:存在
    }
    else 
    {
        @:不存在
    }
}

@functions {
    /// <summary>
    /// 方法必须要求为静态
    /// </summary>
    /// <param name="num"></param>
    /// <returns></returns>
    public static IHtmlString Get(int num) 
    {
        if(num==0)
        {
            return new HtmlString("存在");
        }
        else
        {
            return new HtmlString("不存在");
        }
    }
} 

前台调用:

    @*
    1、引用的时候会出现提示红线报错,但是如果cshtml文件放入在了app_code中并且如果是functions方法内的C#代码类为静态类,引用的空间类名(cshmtl名称)正确的话,运行的时候是不会报错的
    2、htmlstring的含义:表示不应再次进行编码的 HTML 编码的字符串。
    3、放入app_code下面的页面会在编译的时候自动编译为类
    *@
    @IHepler.show(0)
    @IHepler.Get(1)

如此就实现了页面对于方法的复用和方法的封装

原文地址:https://www.cnblogs.com/llcdbk/p/6060772.html