Hidden Html Helper in MVC3 Razor


  • Html.Hidden is the helper which renders a Hidden field.
  • Helpers are not controls by itself, they simply generate html markup.
  • Html.Hidden renders input type = "hidden".
It has following overloads :

Overload 1 :


This overload accepts single parameter i.e. string name. This passed name is used as name and id attribute.

Syntax :

<p>
     @Html.Hidden("Hidden")
</p>


Rendered HTML :


<p>
   <input id="Hidden" name="Hidden" type="hidden" value="" />
</p>

The name passed in the helper method is set as name and id attribute.


Overload 2 :




This overload accepts two parameters, name and value parameter. The value parameter sent is use to set the value attribute of the control.


Syntax :



<p>
     @Html.Hidden("Hidden","This is hidden field")
</p>


Rendered HTML :


<p>
   <input id="Hidden" name="Hidden" type="hidden" value="This is hidden field" />
</p>

The value parameter passed is set as value attribute. This value attribute is accessed when form is posted.


Overload 3 :




This overload accepts three parameters. This overload accepts htmlAttributes object.


Syntax :

<p>
     @Html.Hidden("Hidden", "This is hidden field", new {isHidden = true })
</p>

Rendered HTML :


<p>
   <input id="Hidden" isHidden="True" name="Hidden" type="hidden" value="This is hidden field" />
</p>

We have assigned a custom attribute in htmlAttributes object. The attributes is set accordingly.


Overload 4 :




This overload accepts same number of parameters as previous one. This overload accepts IDictionary object of htmlAttributes instead of object htmlAttributes.

Syntax :


<p>
     @Html.Hidden("Hidden", "This is hidden field", hidden)
</p>

We have created a IDictionary object of attributes and is passed as third parameter.

Rendered HTML :

<p>
   <input id="Hidden" isHidden="True" name="Hidden" type="hidden" value="This is hidden field" />
</p>

0 comments:

Post a Comment