I have a helper function that turns minutes into hours/mins. I currently have it in my layout.cshtml but each page cannot see the function. Where should I put the helper function such that every page can see it?
@helper DisplayElapsedTime(int timeInMins){ String timeStr = ""; if (timeInMins >= 60) { int hours = timeInMins/60; timeInMins -= hours * 60; timeStr = hours + "h "; } if (timeInMins > 0){ timeStr += timeInMins + "m"; } @timeStr; }
The HtmlHelper class renders HTML controls in the razor view. It binds the model object to HTML controls to display the value of model properties into those controls and also assigns the value of the controls to the model properties while submitting a web form.
Razor is a markup syntax that lets you embed server-based code into web pages using C# and VB.Net. It is not a programming language. It is a server side markup language. Razor has no ties to ASP.NET MVC because Razor is a general-purpose templating engine. You can use it anywhere to generate output like HTML.
HTML Helpers are managed within View to execute HTML content. We can use HTML Helpers to implement a method that returns a string. This tutorial discusses how you can define or create custom HTML Helpers that you can use within your MVC views.
You should put it into the App_Code folder. There is an awesome article for you to read ASP.NET MVC Helpers
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With