Error filling a DropDownListFor There is no ViewData item of type 'IEnumerable

0

I'm having an error while passing the contents of the controller to the view, I'm doing this way, the ViewBag is being populated with the data.

In my application I select the items:

        public List<TB_EMPRESA> ListarTodos()
        {
            var strQuery = "select * from tb_empresa";

            using (contexto = new Contexto())
            {
                var retornoDataReader = contexto.ExecutaComandoComRetorno(strQuery);
                return TransformaReaderEmListaObjetos(retornoDataReader);
            }

        }

no controller:

        // GET: CadastroUsuario
        public ActionResult Index()
        {


            //lista empresa
            var tbuscarEmpresa = new EmpresaAplicacao();
            var listarEmpresa = tbuscarEmpresa.ListarTodos();
            ViewBag.Empresa = listarEmpresa;


            return View();
        }

in View

        <div class="col-md-3 form-group">
                @Html.LabelFor(x => x.tbidempresa)
                @Html.DropDownListFor(x => x.tbidempresa.IDEMPRESA, ViewBag.Empresa as SelectList, "Selecione um item..", new { @class = "form-control" })
                @Html.ValidationMessageFor(x => x.tbidempresa)
            </div>

Error Image:

    
asked by anonymous 14.01.2016 / 22:12

1 answer

1

The following setting is required in the controller:

public ActionResult Index()
        {

            //lista empresa
            var tbuscarEmpresa = new EmpresaAplicacao();
            var listarEmpresa =  tbuscarEmpresa.ListarTodos();
            ViewBag.Empresa = new SelectList( listarEmpresa,"IDEMPRESA", "RAZAO_SOCIAL"); //esta informação com o nome dos campos

            return View();
        }
    
14.01.2016 / 23:46