Friday, April 1, 2011

@helper in Razor view.

          You can create your own custom html helpers in Razor, using a brand new syntax @helper in Razor. Creating a html helper using @helper is very much easier than our current html extension method types.

@helper in Razor view engine.

            This is a sample example for @helper usage. I've create a textbox helper which accepts a name and a value and will create a text box for MVC 3 Unobstructive JavaScript validation. ( I used it for dynamically created views)

   You can read more about @helper in the from here and here

       @helper TextBox(string Name, object Value)
    {
     <input class="text-box single-line required" id='@Name' data-val-required='This field is required' name='@Name' type="text" data-val="true" value='@Value'/>
     <span class="field-validation-valid" data-valmsg-for='@Name' data-valmsg-replace="true">
     </span>     
}
Truncate Helper
@helper Truncate(string input, int length)
{
    if (input.Length <= length) 
        { @input } 
    else 
        { @input.Substring(0, length)<text>..</text>}
}

0 comments:

Post a Comment