- <!--HTML helper method for DropDownList field with ID and OptionLabel -->
- <%= Html.DropDownList("ddlStates") %>
- <%= Html.DropDownList("ddlStates","Please select state") %>
Exception : There is no ViewData item with the key 'ddlStates' of type 'IEnumerable<SelectListItem>'.
Code
- <!--HTML helper method for DropDownList field with ID and SelectListItem ( Made "Maryland" to be the default selected value) -->
- <%= Html.DropDownList("ddlStates",new List <SelectListItem>(){
- new SelectListItem(){ Text="Maryland", Value="MD", Selected=true },
- new SelectListItem(){Text="Kansas", Value="KS"},
- new SelectListItem(){Text="Texas", Value="TX"}}) %>
- <select id="ddlStates" name="ddlStates"><option selected="selected" value="MD">Maryland</option>
- <option value="KS">Kansas</option>
- <option value="TX">Texas</option>
- </select>
- <!--HTML helper method for DropDownList field with ID , SelectListItem and OptionLabel(Please select state). OptionLabel will be selected by default if not selected is given in the SelectListItem-->
- <%= Html.DropDownList("ddlStates", new List <SelectListItem>(){
- new SelectListItem(){ Text="Maryland", Value="MD"},
- new SelectListItem(){Text="Kansas", Value="KS"},
- new SelectListItem(){Text="Texas", Value="TX"}},"Please select state") %>
- <select id="ddlStates" name="ddlStates"><option value="">Please select state</option>
- <option value="MD">Maryland</option>
- <option value="KS">Kansas</option>
- <option value="TX">Texas</option>
- </select>
- <!--HTML helper method for DropDownList field with ID , SelectListItem , OptionLabel(Please select state) and custom attributes. use '@' if the attribute name is keyword in C# or VB. The default selection will be "Maryland".-->
- <%= Html.DropDownList("ddlStates", new List<SelectListItem>(){
- new SelectListItem(){ Text="Maryland", Value="MD", Selected=true },
- new SelectListItem(){Text="Kansas", Value="KS"},
- new SelectListItem(){Text="Texas", Value="TX"}}, "Please select state", new { @class="error", client_selector="alphanumeric"})%>
- <select class="error" client_selector="alphanumeric" id="ddlStates" name="ddlStates"><option value="">Please select state</option>
- <option selected="selected" value="MD">Maryland</option>
- <option value="KS">Kansas</option>
- <option value="TX">Texas</option>
- </select>

No comments:
Post a Comment