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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<html> <body> @helper CretingMultipleRows(int loopvalue) { <table><tr id=@loopvalue><td>Calling helper Method : @loopvalue</td></tr></table> } <table><tr><th>Creting Multiple Row</th></tr> <tbody> @for (int loop_index = 0; loop_index < 5; loop_index++) { @CretingMultipleRows(loop_index) } </tbody> </table> </body> </html> |
Leave A Comment