HTML Helpers in ASP.NET MVC
Built-In Html Helpers
Standard Html Helpers
HTML elements like as HTML text boxes, checkboxes are used.
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>Strongly Typed HTML Helpers
Based on model properties HTML elements are created.
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.
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>
Comments
Post a Comment