views:

62

answers:

5
<select name="MessageType"  style="width: 151px">
    <option value="P">P - Proprietary</option>
    <option 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

+6  A: 

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.

Darin Dimitrov
Yes But I am hard coding the dropdown list values.. in this case?
kumar
Don't hardcode the list values. Use a helper.
Darin Dimitrov
@kumar: you can provide them with your model that will fill them up in the `DropDownListFor()`
Robert Koritnik
I used your Code Darin If I use DropDownListFor its not working for me..
kumar
I am not able to display the Database Value in to the Dropdown list box
kumar
*Not working fine* is not a very precise problem description, at least not one that would allow us to help you much.
Darin Dimitrov
+4  A: 

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> 
hunter
Thanks hunter.. if I try your code I am getting this error message Cannot convert lambda expression to type 'System.Collections.Generic.IEnumerable<Telerik.Web.Mvc.UI.GridColumnSettings>' because it is not a delegate typePlease help me out for solving this issue
kumar
I think you'll need to post more. I assumed `p` was some local variable. What is `p`?
hunter
P is some value.. which is the MessgeType.. P B S are MessageTypes.
kumar
+1  A: 

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.

KeithS
-1: This is MVC not WebForms: Avoid Asp.net WebForms server controls at all costs.
Robert Koritnik
+1  A: 

For example, try:

<option selected="selected">

This and this might help.

KMan
+1  A: 

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.

PolishedTurd