How to fill a DropDownList in mvc c #? [duplicate]

0

In my parameters I have "Option" which are

-Gestion
-Colegio
-Estado
-Pais

I want to upload it to my DropDownList try in this way to load it but it's not like taking it into view, do you have any other method?

Controller:

public ActionResult Index()
        {    
List<SelectListItem> lst = new List<SelectListItem>();

lst.Add(new SelectListItem() { Text = "Gestion", Value = "1" });
lst.Add(new SelectListItem() { Text = "Colegio", Value = "2" });
lst.Add(new SelectListItem() { Text = "Estado", Value = "3" });
lst.Add(new SelectListItem() { Text = "Pais", Value = "4" });

 ViewBag.Opciones = lst;

        return View();
}

my view:

@model TNT.Controllers.ResumenParam

@{
    ViewBag.Title = "Index";
}
@using (Html.BeginForm("Imprimir", "Resumen", new { id = "PDF" }, FormMethod.Post))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <fieldset>
        <div class="editor-field">
            @Html.DropDownListFor(model => model.Gestion, Enumerable.Range(2000, 20).Select(x => new SelectListItem { Text = x.ToString() }))
            @Html.ValidationMessageFor(model => model.Gestion)
        </div>
       
        <div class="editor-field">
            @Html.DropDownList("Opcion", ViewBag.Opciones)
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

Model:

public class CampoResumenColegioParam
{

    //     [Required(ErrorMessage = "Ingrese una Gestion")]
    //     [Display(Name = "Gestio")]
    //  [DataType(DataType.int)]
    public int Gestion { get; set; }
    public int Opciones { get; set; }
}

    
asked by Rodrigo Rodriguez 24.10.2017 в 17:25
source

2 answers

0

Try saving the SelectListItem in a ViewBag and then pass the data to the helper DropDownList .

In your action you would have this:

[HttpGet]
public ActionResult Index()
{
    List<SelectListItem> lst = new List<SelectListItem>();

    lst.Add(new SelectListItem() { Text = "Gestion", Value = "1" });
    lst.Add(new SelectListItem() { Text = "Colegio", Value = "2" });
    lst.Add(new SelectListItem() { Text = "Estado", Value = "3" });
    lst.Add(new SelectListItem() { Text = "Pais", Value = "4" });

   ViewBag.Opciones= lst;

  return View();
}

In your view then to show it would be this:

@Html.DropDownListFor(x=>x.Opciones, (IEnumerable<SelectListItem>)ViewBag.Opciones)
    
answered by 24.10.2017 / 17:43
source
0

One possibility is to create an enumeration with the possible values:

public enum ValoresOpcion
{
    Gestion= 1, Colegio = 2, Estado = 3, Pais = 4
}

The property of your model could be of the enumeration type:

public class Modelo
{
    public ValoresOpcion Opcion { get; set; }
}

And in the view you could use the extension method 'EnumDropDownListFor':

@Html.EnumDropDownListFor(model=> model.Opcion)
    
answered by 24.10.2017 в 17:34