在多个视图共享MVC剃刀功能 [英] Shared MVC Razor functions in several views

查看:144
本文介绍了在多个视图共享MVC剃刀功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由多个页面共享在我看来功能:

I have functions in my view that is shared by several pages:

@functions 
{
    public HtmlString ModeImage(ModeEnum mode) 
    {
        switch(mode)
        {
            case AMode: new HtmlString("<img etc..."); break;
            // more etc...
        }
    }
}

有没有办法把它在一个单独的文件,并将其包含在每一页上,而无需复制并粘贴到每个之一。我知道我可以写一个cs文件以及每页访问它,但是功能确实涉及的观点和I'de讨厌不得不重新编译,如果这个功能的改变。

Is there a way to have it in a separate file and include it on each page without having to copy and paste it in to each one. I know I can write a .cs file and access it per page, but the function really concerns the view and I'de hate to have to recompile if this function changes.

推荐答案

这听起来像你想在博客文章中描述的剃刀@helper方法<一href=\"http://weblogs.asp.net/scottgu/archive/2011/05/12/asp-net-mvc-3-and-the-helper-syntax-within-razor.aspx\">ASP.NET MVC3和由Scott Guthrie的剃刀内@helper语法。

This sounds like you want the Razor @helper methods described in the blog post ASP.NET MVC3 and the @helper syntax within Razor by Scott Guthrie.

下面是概述...
内剃刀@helper语法,您可以轻松创建可重用的辅助方法,可以在视图模板封装输出功能,它们能够更好地code再利用,也可以方便更具可读性code。

这篇关于在多个视图共享MVC剃刀功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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