Cascade and Composite Dropdownlist MVC

Asked

Viewed 211 times

0

I have one question

I have 1 User class and composed by other classes Gender, Course and Semester

Ex

    public int Id { get; set; }
    public string Nome { get; set; }
    public Genero Sexo { get; set; } = new Genero();
    public Curso Curso { get; set; } = new Curso();
    public Semestre Semestre { get; set; } = new Semestre();

And my doubt is in my controller to popular my Dropdownlistfor

My DAO Classes Equal to Course and Semester

    public List<Genero> ListarGenero()
    {
        var Genero = new List<Genero>();
        SqlDataReader reader;

        using (contexto = new Contexto())
        {
            var strQuery = " SELECT * FROM Genero ";
            reader = contexto.ExecutaComandoComRetorno(strQuery);

            while (reader.Read())
            {
                var temObjeto = new Genero()
                {
                    id = int.Parse(reader["Id"].ToString()),
                    Sexo = reader["Genero"].ToString()
                };
                Genero.Add(temObjeto);
            }
        }
        reader.Close();
        return Genero;
    }

My Action

    public ActionResult GetGenero()
    {
        GeneroDAO gen = new GeneroDAO();
        ViewBag.Genero = new SelectList(gen.ListGenero(), "Id", "Sexo");
        return View();
    }

View

     @Html.DropDownListFor(model => model.Sexo.id, (SelectList)ViewBag.Genero, new { @class = "form-control" })

I looked in some forums I found nothing using composition I would like to do how to popular the dropdown coming from the bank and also do cascading in the course, I only found with Javascript but I can not use because it also does not recognize the composition of other classes

Ele apresenta esse erro

  • It wouldn’t just be;@Html.DropDownListFor(model => model.Sexo ...

1 answer

-1

Send in the viewbag the generous list and in the view put

@Html.DropDownListFor(model => model.Sexo.id, new SelectList
(
  ViewBag.Genero, "Id", "Sexo"
),"Insira o sexo", new { @class = "form-control" })

Browser other questions tagged

You are not signed in. Login or sign up in order to post.