Creating reusable method using @helper in MVC Razor

 Creating reusable method using @helper in MVC Razor

The @helper enables you to easily create re-usable helper methods that can encapsulate 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) { … }

Inside the this method we can integrate both HTML and Server-side code

 .cshtml

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

Output :

@helpermethod

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s