0

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()
                           }
                      );
        }

1 Answer 1

2

I would suggest you take a look at ASP.NET MVC Html.DropDownList SelectedValue

Sign up to request clarification or add additional context in comments.

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.