13

I have a MVC4 web app and i currently have a few @helper's that i use on multiple pages, defined within cshtml. The problem is, i have to define them on each page they are used. Is it possible to create a .cshtml file that contains all of my @helper's and include that page into my views?

i.e. this is used on every Index view in the cms area.

@helper DisplayPager(Int32 CurrentPage, Int32 MaxPages) {
    String IsActive = "";
    <div class="pagination pagination-centered">
        <ul>
            <li><a href="@Url.Action("Index", new { id = CurrentPage - 1 > 0 ? CurrentPage - 1 : 1 })">Prev</a></li>
            @for (int i = 0; i < MaxPages; i++)
            {
                IsActive = ((i + 1) == CurrentPage) ? "active" : "";

                <li class="@(IsActive)"><a href="@Url.Action("Index", new { id = i + 1 })">@(i + 1)</a></li>       
            }
            <li><a href="@Url.Action("Index", new { id = CurrentPage + 1 < MaxPages ? CurrentPage + 1 : MaxPages })">Next</a></li>
        </ul>
    </div>
}

Same definition, same code, same everything, but it is in the code at least 15 times now.

bizzehdee
  • 20,289
  • 11
  • 46
  • 76
  • It depends what the body of those methods are, but I would recommend you just have a static class file (`.cs`) that has all your common functions in. You can include that file in your `.cshtml` file with `@include MyProject.MyHelpers` for example. – musefan Aug 06 '13 at 16:15
  • i have updated the code to make it easier to answer – bizzehdee Aug 06 '13 at 16:16
  • The is the ability to define re-usable helper methods using the @helper syntax is available, see the following: http://weblogs.asp.net/scottgu/archive/2011/05/12/asp-net-mvc-3-and-the-helper-syntax-within-razor.aspx – David Tansey Aug 06 '13 at 16:23

1 Answers1

14

Here is instructions to achieve that

This procedure shows you how to create the helper that creates the note, as just described. This is a simple example, but the custom helper can include any markup and ASP.NET code that you need.

  1. In the root folder of the website, create a folder named App_Code. This is a reserved folder name in ASP.NET where you can put code for components like helpers.

  2. In the App_Code folder create a new .cshtml file and name it MyHelpers.cshtml.

  3. Replace the existing content with the following:

    @helper MakeNote(string content) 
    {
      <div class="note" 
           style="border: 1px solid black; width: 90%; padding: 5px; margin-left: 15px;">
        <p>
          <strong>Note</strong>&nbsp;&nbsp; @content
        </p>
      </div>
    }
    

    The code uses the @helper syntax to declare a new helper named MakeNote. This particular helper lets you pass a parameter named content that can contain a combination of text and markup. The helper inserts the string into the note body using the @content variable.

    Notice that the file is named MyHelpers.cshtml, but the helper is named MakeNote. You can put multiple custom helpers into a single file.

  4. Save and close the file.

Copied it from this article

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
Vasanth
  • 1,670
  • 1
  • 13
  • 17
  • 10
    This works, and seems to be the standard way to do it, but it is such a horrible hack. Why Microsoft decided App_Code was the place to put shared helpers in beyond me. Views should be in the Views Folder. This is completely counter-intuitive. – user1751825 Dec 18 '16 at 08:17