Fill a Dropdownlist with an occurrence stored in MVC

1

I want to fill out a dropdwonlist with a stored procedure.

My procedure is as follows:

CREATE PROCEDURE GetDepartamentos
AS
BEGIN
        SELECT 0 Id_Departamento, 'Todos'  Nombre_Departamento
        UNION ALL
        SELECT Id_Departamento, Nombre_Departamento FROM Departamentos
END

I have already mapped my procedure with entityframework and created a model GetDepartamentos_Result.cs

Now in my controller I have a method that returns a List:

public List<GetDepartamentos_Result> GetDepartamentos()
    {
        using (TigerEntities db = new TigerEntities())
        {
            var v = db.GetDepartamentos().ToList();
            return v;
        }
    }

And an ActionResult combo method that sends me Combo.cshtml view:

public ActionResult Combo()
    {
        var a = GetDepartamentos();
        ViewBag.Departamentos  = new SelectList(a, "Id_Departamento", "Nombre_Departamento");
        return View();
    }

Is it correct what I am doing to fill the combo? I have not managed to get it and also as I would call it in view?

@model List

I hope you can give me some guidance on this.

Thank you very much for your help.

    
asked by Luis 10.01.2018 в 20:10
source

1 answer

0

In the view you could do:

@Html.DropDownList("Departamentos")

If you want to have it related to a Model you could do:

@Html.DropDownListFor(model => model.id_Departamento, ViewBag.Departamentos as SelectList)
    
answered by 10.01.2018 / 22:17
source