加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 在几个网页(.cshtml文件)中使用Razor @functions

发布时间:2020-12-15 23:15:29 所属栏目:asp.Net 来源:网络整理
导读:我有以下功能,我想要在我的asp.net网页2应用程序中的几个.cshtml视图.如何使这个函数可用于应用程序中的任何视图(而不只是一个). @functions { public bool DisplayButton(String startDate,String endDate) { return Convert.ToDateTime(startDate) DateTim
我有以下功能,我想要在我的asp.net网页2应用程序中的几个.cshtml视图.如何使这个函数可用于应用程序中的任何视图(而不只是一个).
@functions {

    public bool DisplayButton(String startDate,String endDate)
    {
        return Convert.ToDateTime(startDate) < DateTime.Now && Convert.ToDateTime(endDate) > DateTime.Now;
    }
}

解决方法

在App_Code中创建一个名为Functions.cshtml的文件,然后将您所拥有的代码粘贴到文件中.然后,您可以在任何.cshtml文件中使用文件名前缀DisplayButton方法:
var myBool = Functions.DisplayButton(DateTime.Now,DateTime.Now.AddDays(30));

有关在ASP.NET网页中使用函数和帮助的更多信息,请阅读:http://www.mikesdotnetting.com/Article/173/The-Difference-Between-@Helpers-and-@Functions-In-WebMatrix

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读