views:

47

answers:

1

Hello community,

I create a List with 11 values. The first element contains some text ("Please select ..."), the second is "0", the third is "1" and so on ...

Note, that the 6th element contains "5" (five) in the "Text" and the "Value" property. The 6th element is the only one where the "Selected" property is set to "true".

This works fine for me.

But: How looks the Html.DropDownList(...) in my website to show the 11 values and to pre-select the 6th element?

List<SelectListItem> xValues = new List<SelectListItem>() 
                                   {  new SelectListItem 
                                          {  Selected = false,          // Note: Set to false
                                             Text = "Please select ...",
                                             Value = "Please select ...",
                                          }
                                   };

for (int a = 0; a < 10; a++)
        {
           xValues.Add(new SelectListItem
                           {
                               Selected = ((a==5)?true:false), //Note:The 6th element will be set to true
                               Text = a.ToString(),
                               Value = a.ToString()
                           }
                      );
        }
+2  A: 

I would suggest you take a look at http://stackoverflow.com/questions/624828/asp-net-mvc-html-dropdownlist-selectedvalue

Joakim
Thanks for the fast reply. You are right! I found this solution after reading the post: **<%: Html.DropDownList("xValues", (SelectList)ViewData["xValues"])%>**