一、在mvc项目根目录下建 App_Code 文件夹,在该文件夹下建 Common 公用方法视图
Common.cshtml
@helper GetText(int flag) { if (flag == 1) { @:successful } else { @:fail } } @functions { public static int GetValue(DateTime endTime) { if (DateTime.Now.Date <= endTime.Date) { return 1; } else { return 0; } } }
如下图:
二、视图方法的使用
@Common.GetText(1) @Common.GetValue(new DateTime(2018, 10, 30)) @{ int value = Common.GetValue(new DateTime(2018, 10, 30)); }
如下如: