Home/MVC/Creating reusable method using @helper in MVC Razor

Creating reusable method using @helper in MVC Razor

What is HTML Helper ?

The HTML helper is a method that returns a string. ASP.NET MVC Framework itself contains extension methods for HtmlHelper class to have well structured helper methods separation. Asp .Net MVC allow more flexible to extend the HelmHelper class to create our own custom helper method. It promote the use of reusable code.

More about HTML Helper here.

Example : Reusable method using @helper

The @helper class enables us to easily create re-usable helper methods that can encapsulate in output function. This enable code reuse and more readable. It bind blocks of Markup (HTML) and Server-side logic into reusable page-level methods.

Syntax : @helper MethodName(parameters) { … }

As per below, we are creating a simple helper method in the view (CretingMultipleRows). The main role of the CretingMultipleRows method is to create a table based on the loopvalue input. Based on the code, we can make reuse of this helper method within the view.

Index .cshtml

Output :

reuse helper method in MVC - dotnet-helpers

Leave A Comment