TextAreaFor helper in MVC3 Razor

  • Html.TextAreaFor is the helper which renders a TextArea.
  • Helpers are not controls by itself, they simply generate html markup.
  • Html.TextAreaFor helps in binding control to the model.

It has following overloads :

Overload 1 :


This overload accepts only parameter i.e. linq expression. This expression binds the control to the model's property.

Syntax :

<p>
     @Html.TextAreaFor(c => c.Address)
</p>


UI :



Rendered HTML :

<p>
    <textarea cols="20" id="Address" name="Address" rows="2">
</textarea>
</p>


Overload 2 :



The second overload accepts two parameter, one is the expression and other is the htmlAttibutes object.

Syntax :

<p>
     @Html.TextAreaFor(c => c.Address, new { rows = "3", cols = "25" })
</p>


UI :



Rendered HTML :

<p>
    <textarea cols="25" id="Address" name="Address" rows="3">
</textarea>
</p>

The rows and cols attribute are set as passed in htmlAttributes object.



Overload 3 :




This overload accepts expression and IDictionary object of htmlAttributes.


Syntax :



<p>
     @Html.TextAreaFor(c => c.Address, textarea)
</p>



UI :



Rendered HTML :

<p>
    <textarea cols="25" id="Address" name="Address" rows="3" style="color:Red">
</textarea>
</p>

The attributes passed using IDictionary object has been set as you can see in above code.


Overload 4 :




This overload accepts expression, rows, cols and htmlAttributes object.


Syntax :

<p>
     @Html.TextAreaFor(c => c.Address, 3, 25, new { maxlength = "20"})
</p>


UI :



Rendered HTML :

<p>
    <textarea cols="25" id="Address" name="Address" rows="3" style="color:Red">
</textarea>
</p>


Overload 5 :



Syntax :

<p>
     @Html.TextAreaFor(c => c.Address, 3, 25,textarea)
</p>


UI :



Rendered HTML :

<p>
    <textarea cols="25" id="Address" name="Address" rows="3" style="color:Red">
</textarea>
</p>





0 comments:

Post a Comment