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))
))