0

I am looking for a way where I can define global functions so I can share the implementation across multiple views.

I was wondering where would I place them? I tried _viewStart.cshtml and /Share/_Layout.cshtml but It seems that is not working.

I don't want to create an Extension to the HtmlHelper class. In my case, extensions won't work. I hate to keep copying the same function in multiple views.

@functions  {
  public static string GetColumnHeader(string columnName, string columnHeader, ISupportGridViewModel model)
  {
    return string.Format("{0} {1}", columnHeader, model.Sort == columnName ? model.SortDir == "ASC" ? "▲" : "▼" : string.Empty);
  }
}

For those who commented that I should use extension. Here is the example where I am not able to to get it to work. As far as I know, Razor helpers are nothing more than extension methods and It won't work.

Here is an example that is working

    var grid = new WebGrid(this.Model.Data);
    grid.Pager(WebGridPagerModes.Numeric);
    @grid.GetHtml(tableStyle: "table grid",
                  columns: grid.Columns(
                  grid.Column(columnName: "Time", format: (item) => MethodDefinedInView(item.EndTime))
  ))

Here is an example that doesn't work...

var grid = new WebGrid(this.Model.Data);
grid.Pager(WebGridPagerModes.Numeric);
@grid.GetHtml(tableStyle: "table grid",
              columns: grid.Columns(
              grid.Column(columnName: "Time", format: (item) => Html.ExtensionMethod(item.EndTime))

))

Sam
  • 875
  • 10
  • 22
  • Why won't extensions work? that looks like a good candidate for an extension. – Tim M. Dec 03 '14 at 01:43
  • What makes you think _extensions won't work_? –  Dec 03 '14 at 01:43
  • I am using MVC webgrid and the when you want to pass an extension as ( Func format), It fails. It doesn't like extensions here. So, The only way that I was able to resolve it by invoking a function instead. – Sam Dec 03 '14 at 01:46
  • If you can use the function above, then you can certainly create a HtmlHelper method that does the same - something like `@Html.ColumnHeader(columnName, ....)` –  Dec 03 '14 at 02:21
  • perhaps you can use a razor helper instead, you can read about them here http://weblogs.asp.net/scottgu/asp-net-mvc-3-and-the-helper-syntax-within-razor . It also includes information on how to reuse them. Your problem with reuse is probably that you're making the function static. – Erik Funkenbusch Dec 03 '14 at 05:48
  • I Updated the question. Extension methods are not working. – Sam Dec 03 '14 at 16:33
  • @Sam - razor helpers are not extension methods. – Erik Funkenbusch Dec 05 '14 at 06:07
  • When I think of razor helpers, i think of Html.TextBox/TextBoxFor/DropDownFor/etc. Those are helpers and they are extensions methods to the HtmlHelper class. – Sam Dec 05 '14 at 13:08

1 Answers1

2

Just create a static method in a Utilities class. You can access that in your view by either adding a reference to the namespace in the Web.config inside the Views folder, or just at the top of each cshtml file.

krillgar
  • 12,596
  • 6
  • 50
  • 86