Guest User

Untitled

a guest
May 16th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. @Html.DropDownList("TipoIdentificacionId", null, htmlAttributes: new { @class = "form-control" })
  2.  
  3. public ActionResult Index()
  4. {
  5. //Creo una lista de ejemplo
  6. List<SelectListItem> opciones = new List<SelectListItem>()
  7. {
  8. new SelectListItem{ Text="Texto 1", Value="Value 1" },
  9. new SelectListItem{ Text="Texto 2", Value="Value 2" },
  10. new SelectListItem{ Text="Texto 3", Value="Value 3" },
  11. new SelectListItem{ Text="Texto 4", Value="Value 4" }
  12. };
  13.  
  14. //Recorro la lista (que en tu caso vendría creada de otro lugar, asumo
  15. foreach(var opcion in opciones)
  16. {
  17. //Evalúo la condición que me llevaría a hacer que esa opción este seleccionada
  18. if (opcion.Text.Contains("4"))
  19. {
  20. //La selecciono
  21. opcion.Selected = true;
  22. }
  23. }
  24. //Por último la asigno a un ViewBag y retorno la vista
  25. ViewBag.opciones = opciones;
  26.  
  27. return View();
  28. }
  29.  
  30. @Html.DropDownList("prueba",(List<SelectListItem>)ViewBag.opciones,null,null)
  31.  
  32. @Html.DropDownListFor(x => x.tu_variable, (List<SelectListItem>)ViewBag.opciones,null,null)
Add Comment
Please, Sign In to add comment