Dropdownlist and Listbox - Recovering Values in Editing

Asked

Viewed 531 times

1

In a CMS I own a DropDownList and a ListBox that work regularly in the field of registration, however, do not bring values in the field of editing.

View

<div>
    Tipo de Curso<br />
    @Html.DropDownList("TipoCurso", ViewData["listTipoCurso"] as SelectList)
</div>

<div>
    Promoções <br /> 
    @Html.ListBox("Promocoes", 
        new MultiSelectList(ViewData["Promocoes"] as System.Collections.IEnumerable, 
        "id", "nome"), new 
        { 
            @class = "chosen-select", 
            @placeholder = "Escolha uma Opção" 
        })
</div>

Controller

TipoCursoAplicacao bdTipoCurso;
bdTipoCurso = TipoCursoAplicacaoConstrutor.TipoCursoAplicacaoEF();
var listTipoCurso = new SelectList(bdTipoCurso.ListarTodos()
    .GroupBy(x => x.Nome.ToLower().Trim()).Select(y => y.First()), 
    "ID", "Nome");
ViewData["listTipoCurso"] = listTipoCurso;

PromocoesAplicacao bdPromocoes;
bdPromocoes = PromocoesAplicacaoConstrutor.PromocoesAplicacaoEF();
ViewData["Promocoes"] = bdPromocoes.ListarTodos();

How could you proceed to bring selected data registered in Listbox and in the Dropdownlist at editing time?

  • The problem is that Typocurso and Promocoes are foreign keys and my View is typed for Courses

  • 1

    It worked. Thank you.

1 answer

1


Solved thanks to the help I received in the comments.

View

<div>
    Tipo de Curso <br /> 
    @Html.DropDownListFor(model => model.TipoCurso.ID, ViewData["listTipoCurso"] as SelectList)
</div>

Controller

TipoCursoAplicacao bdTipoCurso;
bdTipoCurso = TipoCursoAplicacaoConstrutor.TipoCursoAplicacaoEF();
Cursos.TipoCurso = bdTipoCurso.ListarPorId(collection["TipoCurso.ID"]);

Browser other questions tagged

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