开发者

MVC: Html.DropDownList(...) question

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
                           {
                               S开发者_开发知识库elected = ((a==5)?true:false), //Note:The 6th element will be set to true
                               Text = a.ToString(),
                               Value = a.ToString()
                           }
                      );
        }


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜