ASP.NET MVC dropdownlist no selecciona valor en el render
Tengo este problema molesto donde mi DropDownlist no selecciona el valor actual por defecto.
Controlador:
var YearsCycling = new SelectList(new List<SelectListItem>()
{
new SelectListItem(){ Value="1yr", Text="1yr"},
new SelectListItem(){ Value="1-3yrs", Text="1-3yrs"},
new SelectListItem(){ Value="3-5yrs", Text="3-5yrs"},
new SelectListItem(){ Value="5-10yrs", Text="5-10yrs"},
new SelectListItem(){ Value="10+yrs", Text="10+yrs"}
},
"Value",
"Text",
new SelectListItem() { Value = "5-10yrs", Text = "5-10yrs",
Selected = true });
ViewBag.YearsCycling = YearsCycling;
Ver:
<%:Html.DropDownListFor(model=>model.YearsCycling,(SelectList)ViewBag.YearsCycling,"-select-") %>
pero en lugar de seleccionar "5-10 años", solo muestra la opción "-seleccionar" y si inspecciono la fuente DOM, no se selecciona ninguno de los elementos.
ACTUALIZACIÓN: Todavía no sé cuál es el problema, pero lo tengo funcionando por ahora haciendo algo feo:
<%
var sl = (SelectList)ViewBag.YearsCycling;
%>
<select name="YearsCycling" id="YearsCycling">
<%foreach(var li in sl){ %>
<option value="<%:li.Value %>" <%if(li.Selected){%>selected="true"<%}%>><%:li.Text %></option>
<%} %>
</select>
Esta no es la mejor solución, pero si has llegado a esta pregunta porque te has quitado el pelo, esto te ayudará.