These helpers are used to render the most common types of HTML elements in strongly typed view like as HTML text boxes, checkboxes etc. The HTML elements are created based on model properties.
The strongly typed HTML helpers work on lambda expression. The model object is passed as a value to lambda expression, and you can select the field or property from model object to be used to set the id, name and value attributes of the HTML helper. A list of most common strongly-typed html helpers is given below:
HTML Element | Example |
---|---|
TextBox | @Html.TextBoxFor(m=>m.Name) Output: <input id=”Name” name=”Name” type=”text” value=”Name-val” /> |
TextArea | @Html.TextArea(m=>m.Address , 5, 15, new{})) Output: <textarea cols=”15″ id=”Address” name=” Address ” rows=”5″>Addressvalue</textarea> |
Password | @Html.PasswordFor(m=>m.Password) Output: <input id=”Password” name=”Password” type=”password”/> |
Hidden Field | @Html.HiddenFor(m=>m.UserId) Output: <input id=” UserId” name=” UserId” type=”hidden” value=”UserId-val” /> |
CheckBox | @Html.CheckBoxFor(m=>m.IsApproved) Output: <input id=”Checkbox1″ name=”Checkbox1″ type=”checkbox” value=”true” /> <input name=”myCheckbox” type=”hidden” value=”false” /> |
RadioButton | @Html.RadioButtonFor(m=>m.IsApproved, “val”) Output: <input checked=”checked” id=”Radiobutton1″ name=”Radiobutton1″ type=”radio” value=”val” /> |
Drop-down list | @Html.DropDownListFor(m => m.Gender, new SelectList(new [] {“Male”, “Female”})) Output: <select id=”Gender” name=”Gender”> <option>Male</option> <option>Female</option> </select> |
Multiple-select | Html.ListBoxFor(m => m.Hobbies, new MultiSelectList(new [] {“Cricket”, “Chess”})) Output: <select id=”Hobbies” multiple=”multiple” name=”Hobbies”> <option>Cricket</option> <option>Chess</option> </select> |