Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where should I locate shared @helper functions in MVC Razor

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; } 
like image 579
Chris Avatar asked Sep 04 '12 14:09

Chris


People also ask

What is @helper in Cshtml?

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.

What is Razor syntax in MVC?

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.

What are helpers in MVC?

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.


1 Answers

You should put it into the App_Code folder. There is an awesome article for you to read ASP.NET MVC Helpers

like image 77
petro.sidlovskyy Avatar answered Sep 23 '22 21:09

petro.sidlovskyy