0
<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

Robert Koritnik
  • 103,639
  • 52
  • 277
  • 404
kumar
  • 2,944
  • 18
  • 60
  • 89

5 Answers5

7

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
  • 1,023,142
  • 271
  • 3,287
  • 2,928
4

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
  • 62,308
  • 19
  • 113
  • 113
  • Thanks hunter.. if I try your code I am getting this error message Cannot convert lambda expression to type 'System.Collections.Generic.IEnumerable' because it is not a delegate type Please help me out for solving this issue – kumar Sep 22 '10 at 17:00
  • I think you'll need to post more. I assumed `p` was some local variable. What is `p`? – hunter Sep 22 '10 at 17:04
  • P is some value.. which is the MessgeType.. P B S are MessageTypes. – kumar Sep 22 '10 at 17:11
1

For example, try:

<option selected="selected">

This and this might help.

Community
  • 1
  • 1
KMån
  • 9,896
  • 2
  • 31
  • 41
1

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.

asfsadf
  • 3,822
  • 7
  • 31
  • 41
0

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
  • 70,210
  • 21
  • 112
  • 164