开发者

How do I a preselect an item in a List(Of SelectListItem) for DropDownList in ASP.NET MVC VB.NET

I have a List(Of SelectListItem) and I fill it with NativeName as .Text and the two letter ISO region name as .Value.

Dim countryList As IList(Of System.Globalization.RegionInfo) = GetRegi开发者_C百科onInfosForEuOnly() Dim dropDownCountryList As New List(Of SelectListItem)

For i As Integer = 0 To countryList.Count - 1
  dropDownCountryList.Add(New SelectListItem() With {.Text = countryList(i).NativeName, .Value = countryList(i).TwoLetterISORegionName})
Next

...

<td>
      <%=Html.DropDownList(customerType & "CountryCode", dropDownCountryList)%>*
      <%=Html.ValidationMessage(customerType & "CountryCode")%>
</td>

Now I want to set the RegionInfo for Germany as the preselected Item in the DropDownList. But

dropDownCountryList.Item(4).Selected = True

doesn't work.

Any Ideas?


How about the use of the SelectList class?


A selectlistItem has a selected value as well I'm sure so you should be able to set that

if(language == german)
{
dropDownCountryList.Add(New SelectListItem() With {.Text = countryList(i).NativeName, .Value = countryList(i).TwoLetterISORegionName, .Selected = true})
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜