How to create Html Helper using Razor declaration Syntax ?


We can create the Html Helper using Razor declaration syntax like this

Step 1: In App_Code folder add new razor view form i.e CustomHelper.cshtml and write the reusable helper method like this


@helper Truncate(string input, int length)
{
    if (input.Length <= length)
    {
    @input
    }
    else
    {
    @input.Substring(0, length)<text>........ </text>
    }
}

Step 2: Call the Html hepler in View page of application like this

@{
    ViewBag.Title = "Index";
}

<b>Htmlhelper using Razor declaration Helper Sysntax </b>


<br />
<br />

@CustomHelper.Truncate(@ViewBag.Message as string, 7) 
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

%d bloggers like this: