<select name="MessageType" style="width: 151px">
<option value="P">P - Proprietary</option>
<o开发者_如何学运维ption value="B">B - BCBSA</option>
<option value="S">S - Place Specific</option>
</select>
How to set the selected value for this Dropdownlist box?
<%=p.MessageType%>
.. this is the value I am getting from database so that In my Grid what ever the Value coming form database it will show in the Dropdownlistbox on the Grid now its showing me as Default value P even Database value B
Thanks
I would recommend you using the standard HTML helper method (Html.DropDownListFor
) for generating the select
field.
<%= Html.DropDownListFor(x => x.MessageType, new SelectList(new[] {
new { Id = "P", Value = "P - Proprietary" },
new { Id = "B", Value = "B - BCBSA" },
new { Id = "S", Value = "S - Place Specific" },
}, "Id", "Value"), new { style = "width: 151px" }) %>
Then simply set the MessageType
property on your view model to any of the possible values (P, B, S) and the helper will take care of the rest.
Quick and dirty way:
<select name="MessageType" style="width: 151px">
<option value="P"<%=p.MessageType == "P" ? "selected=\"selected\"" : "" %>>P - Proprietary</option>
<option value="B"<%=p.MessageType == "B" ? "selected=\"selected\"" : "" %>>B - BCBSA</option>
<option value="S"<%=p.MessageType == "S" ? "selected=\"selected\"" : "" %>>S - Place Specific</option>
</select>
For example, try:
<option selected="selected">
This and this might help.
In your view model you can have an object that contains the full collection of MessageTypes, then name your DDL to the foreign key of your main Message table, thus allowing it to take advantage of the built-in binding.
<select name="Message.TypeId" id="Message_TypeId" style="width: 151px">
<option value="P">P - Proprietary</option>
<option value="B">B - BCBSA</option>
<option value="S">S - Place Specific</option>
</select>
Assuming your model contains a message object,
Model.Message.TypeId will correspond to, and highlight the appropriate DDL option.
First, this is a standard HTML dropdown. If you want to work with it as a .NET object, it needs to be an asp:DropDownList, and then you can access the selection using SelectedItem or SelectedValue.
For HTML dropdowns, simply add the "selected" attribute to the option element you wish to specify it as the initially-selected value.
精彩评论