Shared MVC Razor functions in several views
Asked Answered
T

3

39

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...
        }
    }
}

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.

Threemaster answered 14/6, 2011 at 17:18 Comment(4)
What's wrong with recompiling?Pharyngo
what about putting this function inside Layout viewLussier
@Miro: That won't work. Pages don't inherit anything from their layout pages. After all, Layout is set at runtime.Pharyngo
See #17603007 if you really need to reuse the same function over several distinct views.Durer
I
20

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.

Here is the overview... "The @helper syntax within Razor enables you to easily create re-usable helper methods that can encapsulate output functionality within your view templates. They enable better code reuse, and can also facilitate more readable code."

Indusium answered 14/6, 2011 at 17:28 Comment(4)
It doesn't look like he wants a helper (he's returning void)Pharyngo
Sorry, it is sort of a helper method which returns an image html depending on an enum. I feel like its too little for a partial and I don't want code to be recompiled if I say, change the icon name. This looks like what I'm looking for. Going to try it out first.Threemaster
That was perfect. =) I didn't know this existed. Thank you very much.Threemaster
This is useful, but helpers have some limitations such as lack of generics, and you can still reuse static methods across multiple views.Endomorphic
E
46

(Here's a more detailed version of the existing answers.)

Create a folder called App_Code in the root of the MVC project if it doesn't already exist. In here, create an empty razor view and name it whatever you want:

MVC project with Razor view called Shared.cshtml inside App_Code folder

Add @helpers and/or static methods to it as needed:

@helper ShowSomething()
{
    <span>Something</span>
}

@functions
{
    public static int CalculateSomething()
    {
        return 1;
    }
}

Then use them from your views by first accessing the shared view by name:

@Shared.ShowSomething()
@Shared.CalculateSomething()
Endomorphic answered 12/11, 2014 at 2:51 Comment(4)
That's awesome. Can you do the same for an Area? Basically, not shared across the whole codebase, but rather just one section?Jat
@twsmale, I don't know; I haven't really used areas before. Maybe you could make a new question for that.Endomorphic
up for explaining in detail about the difference between helper and functions and the way to use themScroggins
now, we cannot share functions in dotnet core .net6 like thisPerfecto
I
20

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.

Here is the overview... "The @helper syntax within Razor enables you to easily create re-usable helper methods that can encapsulate output functionality within your view templates. They enable better code reuse, and can also facilitate more readable code."

Indusium answered 14/6, 2011 at 17:28 Comment(4)
It doesn't look like he wants a helper (he's returning void)Pharyngo
Sorry, it is sort of a helper method which returns an image html depending on an enum. I feel like its too little for a partial and I don't want code to be recompiled if I say, change the icon name. This looks like what I'm looking for. Going to try it out first.Threemaster
That was perfect. =) I didn't know this existed. Thank you very much.Threemaster
This is useful, but helpers have some limitations such as lack of generics, and you can still reuse static methods across multiple views.Endomorphic
P
4

You could make a static helper page and put a normal static method in the page.
You could then call it by writing PageName.MyMethod() anywhere, and I believe that you won't need to recompile the project.

Pharyngo answered 14/6, 2011 at 17:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.