Here is the controller code:
EfCategoriaRepository efCategoriaRepository = new EfCategoriaRepository();
model.Categoria= new List<SelectListItem>();
foreach (var categoria in efCategoriaRepository.FindAllCategorias())
{
model.Categoria.Add(new SelectListItem {
Text = categoria.Nombre,
Value = categoria.CategoriaId.ToString(),
Selected = producto.CategoriaId == categoria.CategoriaId
});
}
Using the debugger, I can confirm that the correct value is being set to "Selected = True".
My model:
public class ProductoModel
{
public int ProductoId { get; set; }
public int CategoriaId { get; set; }
public List<SelectListItem> Categoria { get; set; }
[Required]
public string Nombre { get; set; }
[Required]
public string Titulo { get; set; }
[Required]
[AllowHtml]
public string Descripcion { get; set; }
[Required]
[AllowHtml]
public string Caracteristicas { get; set; }
[Required]
[DisplayName("Precio - Categoria 1")]
public decimal PrecioCatUno { get; set; }
[Required]
[DisplayName("Precio - Categoria 2")]
public decimal PrecioCatDos { get; set; }
[Required]
[DisplayName("Precio - Categoria 3")]
public decimal PrecioCatTres { get; set; }
}
In my editor template:
<div class="input-field">
@Html.LabelFor(m => m.Categoria)
@Html.DropDownListFor(model => model.CategoriaId, Model.Categoria)
</div>
Anybody know why the correct dropdown value isn't being selected despite the fact that I have set "selected" to true on the correct value?