HiddenFor helper in MVC3 Razor

  • Html.HiddenFor is the helper which renders a Hidden field.
  • Helpers are not controls by itself, they simply generate html markup.
  • Html.HiddenFor renders input type = "hidden".
  • It binds the control to the model property. It created stongly binded view.


It has following overloads :


This overload accepts linq expression, which binds the control to the model property.


Syntax :

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


Rendered HTML :


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


Overload 2 :



This overload accepts two parameter, i.e. expression and htmlAttributes object.


Syntax :

<p>
     @Html.HiddenFor(c => c.Address, new { isHidden = true })
</p>

Rendered HTML :


<p>
   <input id="Address" isHidden="True" name="Address" type="hidden" value="" />
</p>



Overload 3 :



This overload is similar to the previous one, it accepts IDictionary object of htmlAttributes instead of htmlAttributes object.

Syntax :


<p>
     @Html.HiddenFor(c => c.Address, hidden)
</p>



Rendered HTML :


<p>
   <input id="Address" isHidden="True" name="Address" type="hidden" value="" />
</p>


We have created an IDictionary object of htmlAttributes and assigned a custom attribute named isHidden to it. We can see in Rendered HTML that the attribute is created and set with respective value we have passed.


1 comments: