Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Html.DropDownList("TipoIdentificacionId", null, htmlAttributes: new { @class = "form-control" })
- public ActionResult Index()
- {
- //Creo una lista de ejemplo
- List<SelectListItem> opciones = new List<SelectListItem>()
- {
- new SelectListItem{ Text="Texto 1", Value="Value 1" },
- new SelectListItem{ Text="Texto 2", Value="Value 2" },
- new SelectListItem{ Text="Texto 3", Value="Value 3" },
- new SelectListItem{ Text="Texto 4", Value="Value 4" }
- };
- //Recorro la lista (que en tu caso vendría creada de otro lugar, asumo
- foreach(var opcion in opciones)
- {
- //Evalúo la condición que me llevaría a hacer que esa opción este seleccionada
- if (opcion.Text.Contains("4"))
- {
- //La selecciono
- opcion.Selected = true;
- }
- }
- //Por último la asigno a un ViewBag y retorno la vista
- ViewBag.opciones = opciones;
- return View();
- }
- @Html.DropDownList("prueba",(List<SelectListItem>)ViewBag.opciones,null,null)
- @Html.DropDownListFor(x => x.tu_variable, (List<SelectListItem>)ViewBag.opciones,null,null)
Add Comment
Please, Sign In to add comment