Do a query and see in a dropdownList

1

You are giving the error below in dropdowList and I do not understand why.

Error:

  

Can not bind the object of type 'System.Collections.Generic.List1 [

asked by anonymous 15.12.2015 / 17:42

1 answer

3

ViewBag.Catequizando is not a SelectList . It's a normal list. You need to convert to work. There are two ways to do it:

First, in Controller :

ViewBag.Catequizando = new SelectList(queryNomeCatequizando, "CatequizandoID", "Nome");

Second way, in View :

@Html.DropDownListFor(model => model.CatequizandoID, ((IEnumerable<CatequizandoViewModel>)ViewBag.Catequizando).Select(option => new SelectListItem {
    Text = option.Nome,
    Value = option.CatequizandoID.ToString(),
    Selected = (Model != null) && (Model.CatequizandoID == option.CatequizandoID)
}), htmlAttributes: new { @class = "form-control" })  

In the second form, you must create CatequizandoViewModel before.

    
15.12.2015 / 17:49