There is no Viewdata item of type 'Ienumerable<Selectlistitem>' that has the key 'Usuario.T003_FK_COORDENACAO'

Asked

Viewed 426 times

0

I’m having a problem with Dropdownlistfor, it’s populated with database data.

Controller Code.

var coordenacaoList = db.VW004_LISTAR_COORDENACAO.ToList();
        ViewBag.CoordenacaoList = new SelectList(coordenacaoList, "ID_COORDENACAO", "TITULO_COORDENACAO");

View.

@Html.DropDownListFor(model => model.Usuario.T003_FK_COORDENACAO, (SelectList)ViewBag.CoordenacaoList, " Selecione uma Coordenação", new { @class = "form-control" })

Only when I’m going to post on a trial, give me that mistake.

Imagem de Erro

Controller.

[HttpPost]
    public ActionResult Cadastrar(T001_USUARIO obj)
    {
        try
        {

            SqlParameter param1 = new SqlParameter("@MATRICULA_CADASTRADA", obj.T001_MATRICULA);
            SqlParameter param2 = new SqlParameter("@NOME", obj.T001_NOME_USUARIO);
            SqlParameter param3 = new SqlParameter("@PERFIL", obj.T002_FK_PERFIL);
            SqlParameter param4 = new SqlParameter("@COORD", obj.T003_FK_COORDENACAO);
            var db = new AppModel();

            var data = db.Database.ExecuteSqlCommand("SP001_CADASTRA_USUARIO @MATRICULA_CADASTRADA, @NOME, @PERFIL, @COORD", param1, param2, param3, param4);

            return RedirectToAction("Listar");

        }
        catch
        {           
            return View();
        }
    }

Controller GET.

public ActionResult Cadastrar()
    {

        AppModel db = new AppModel();

        var coordenacaoList = db.T003_COORDENACAO.ToList();
        ViewBag.CoordenacaoList = new SelectList(coordenacaoList, "T003_ID_COORDENACAO", "T003_DESC_COORD");

        var perfilList = db.VW005_LISTAR_PERFIL.ToList();
        ViewBag.PerfilList = new SelectList(perfilList, "T002_ID_PERFIL", "T002_DESC_PERFIL");


        return View();
    }
  • Lucas, please enter the code of your model.

  • public partial class T001_USUARIO { public int T001_ID_USUARIO { get; set; } public string T001_MATRICULA { get; set; } public string T001_NOME_USUARIO { get; set; } public Nullable<bool> T001_ATIVO { get; set; } public Nullable<int> T002_FK_PERFIL { get; set; } public Nullable<int> T003_FK_COORDENACAO { get; set; } }

1 answer

0

Lucas, Good afternoon

I believe this error is happening on the return of your Post method, because the list "(Selectlist)Viewbag.Coordinatacaolist" should be null, make sure that all lists have values before returning to view.

In your Httppost way

[HttpPost]
public ActionResult Cadastrar(T001_USUARIO obj)
{
    try
        {
            AppModel db = new AppModel();

            SqlParameter param1 = new SqlParameter("@MATRICULA_CADASTRADA", obj.T001_MATRICULA);
            SqlParameter param2 = new SqlParameter("@NOME", obj.T001_NOME_USUARIO);
            SqlParameter param3 = new SqlParameter("@PERFIL", obj.T002_FK_PERFIL);
            SqlParameter param4 = new SqlParameter("@COORD", obj.T003_FK_COORDENACAO);
            var db = new AppModel();

            var data = db.Database.ExecuteSqlCommand("SP001_CADASTRA_USUARIO @MATRICULA_CADASTRADA, @NOME, @PERFIL, @COORD", param1, param2, param3, param4);

            return RedirectToAction("Listar");

        }
        catch (Exception ex)
        {         

            var coordenacaoList = db.T003_COORDENACAO.ToList();
    ViewBag.CoordenacaoList = new SelectList(coordenacaoList, "T003_ID_COORDENACAO", "T003_DESC_COORD");

    var perfilList = db.VW005_LISTAR_PERFIL.ToList();
    ViewBag.PerfilList = new SelectList(perfilList, "T002_ID_PERFIL", "T002_DESC_PERFIL");


            return View(obj);
        }
    }
  • You could tell me how to do that, I’m new at C#.

  • Voce can for example add the same code snippet as Voce user in the "Httpget" Action. &#xA;&#xA; var coordenacaoList = db.VW004_LISTAR_COORDENACAO.ToList();&#xA; ViewBag.CoordenacaoList = new SelectList(coordenacaoList, "ID_COORDENACAO", "TITULO_COORDENACAO");&#xA;&#xA;adicione o codigo acima antes do Return View()

  • I don’t understand how to do this, I’ll edit my question and put the controller for you take a look.

  • Also add the Httpget method

  • I’m doing 2 Dropdownlist

  • Did not work, falls straight into the catch, does not display error message.

  • before was already falling in Try catch because otherwise it would not return to the same page, but even so now Voce can see through the variable ex which was the error

Show 2 more comments

Browser other questions tagged

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