在多个网页中使用Razor @functions(.cshtml文件) [英] Using a Razor @functions in several webpages (.cshtml files)

查看:275
本文介绍了在多个网页中使用Razor @functions(.cshtml文件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下功能,希望对我的asp.net网页2应用程序中的多个.cshtml视图可用.如何使此功能可用于应用程序中的任何视图(而不是一个视图).

I have the below function that I would like to be available to several .cshtml views in my asp.net web pages 2 application. How can I make this function available to any view in the application (as opposed to just one).

@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方法:

Create a file called Functions.cshtml in App_Code and then paste the code you have into the file. Then you can call the DisplayButton method in any .cshtml file by prefixing it with the file name:

var myBool = Functions.DisplayButton(DateTime.Now, DateTime.Now.AddDays(30));

有关在ASP.NET网页中使用功能和助手的更多信息,请阅读以下内容:

For more on working with functions and helpers in ASP.NET Web Pages, read this: http://www.mikesdotnetting.com/Article/173/The-Difference-Between-@Helpers-and-@Functions-In-WebMatrix

这篇关于在多个网页中使用Razor @functions(.cshtml文件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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