RadioButtonFor Helper in MVC3 Razor

  1. Html.RadioButtonFor is the helper used to render a RadioButton control.
  2. Helpers are not controls by itself, they simply generate html markup.
  3. Html.RadioButtonFor is rendered as input type = "radio".
  4. Html.RadioButtonFor allows for model biding.
It has following overloads :

Overload 1 :


This overload accepts linq expression and the object value. The linq expression binds the control to model.

Syntax :

<p>
     @Html.RadioButtonFor(c => c.Gender,"Male")
</p>

UI :



Rendered HTML :

<p>
    <input id="Gender" name="Gender" type="radio" value="Male" />
</p>

The display name value of the Gender property becomes the id and name attribute. 



Overload 2 :




This overload accepts three parameters one more than previous overload. 

Syntax :


<p>
     @Html.RadioButtonFor(c => c.Gender, "Male", new { @class = "radiofor" })
</p>

UI :



Rendered HTML :



<p>
    <input class="radiofor" id="Gender" name="Gender" type="radio" value="Male" />
</p>



Overload 3 :



This overload accepts IDictionary object of attributes.

Syntax :

<p>
     @Html.RadioButtonFor(c => c.Gender, "Male",radio)
</p>



UI :



Rendered HTML :

<p>
    <input class="radio" id="Radiobutton" name="Gender" type="radio" value="Male" />
</p>

0 comments:

Post a Comment