Where should I locate shared @helper functions in MVC Razor

大兔子大兔子 提交于 2019-12-03 08:08:40

问题


I have a helper function that turns minutes into hours/mins. I currently have it in my layout.cshtml but each page cannot see the function. Where should I put the helper function such that every page can see it?

@helper DisplayElapsedTime(int timeInMins){
    String timeStr = "";
    if (timeInMins >= 60) {
        int hours = timeInMins/60;
        timeInMins -= hours * 60;
        timeStr = hours + "h ";
    }
    if (timeInMins > 0){
        timeStr += timeInMins + "m";
    }
    @timeStr;
}

回答1:


You should put it into the App_Code folder. There is an awesome article for you to read ASP.NET MVC Helpers



来源:https://stackoverflow.com/questions/12265512/where-should-i-locate-shared-helper-functions-in-mvc-razor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!