views:

217

answers:

2

I have a dropdownlist with values -1,1,2 and text A,B,C i wanna set B as selectedValue by default when the page is loaded.

Something which can be done at the Page Level i.e in aspx or ascx.

+3  A: 

You need to specify "selected" attribute of html "select" tag.

One option is - render select tag on your own.

Second - make sure that you form correct SelectItemList ('datasource' for your dropdownlist) - it must contain one item with .Selected=true.

Arnis L.
yes the second one,which i am unable to set even though i store it as SelectList(collection,"ID","Name",1) in the viewdata and bring it back in the view page it shows me the 0 element instead of 1st element.
Is value of item with index 1 equals with 1? Check out this for more details - http://codeclimber.net.nz/archive/2009/08/10/how-to-create-a-dropdownlist-with-asp.net-mvc.aspx
Arnis L.
I got 3 different dropdown list and for one of them its setting the selectedvalue for others its not. what could be the problem ??
A: 

Haven't checked for syntax but you can do something like this in your get controller...

ViewData[ddlItems] = new SelectList(new List<string>() { "1", "1", "2", "A", "B", "C"}, "B");

And in your view...

<%= Html.DropDownList("ddlItems", (SelectList)ViewData[ddlItems], String.Empty, null)%>
RailRhoad