Error submitting form with ASP MVC

0

I'm having a problem submitting a form developed in ASP MVC. Clicking the save button returns the following error message:

System.InvalidOperationException: 'Não existe item ViewData do tipo 'IEnumerable<SelectListItem>' que possui a chave 'DepartamentoSelecionado'.'

What can be this error?

The project is divided into 4 main files. The first is Model

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace projetinho.Models
{
    public class UsuarioModel
    {
        public int Id { get; set; }

        [Required(ErrorMessage = "Um nome deverá ser informado!")]
        [StringLength(maximumLength: 50, MinimumLength = 3, ErrorMessage ="O nome deve conter no mínimo 3 e no máximo 50 caracteres.")]
        public string Nome { get; set; }

        public int Id_Departamento { get; set; }

        public List<SelectListItem> DepartamentoSelecionado { get; set; }

        public UsuarioModel()
        {
        DepartamentoSelecionado = new List<SelectListItem>();
        }   
    }
}

The second file is a class that will manipulate the database

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace projeto.Models
{
public class UsuarioHandler
{
    private SqlConnection con;
    private void Conexao()
    {
        string stringConexao = ConfigurationManager.ConnectionStrings["sqlServer"].ToString();
        con = new SqlConnection(stringConexao);
    }

    public bool CadastraUsuario(UsuarioModel Usulist)
    {
        Conexao();
        string query = "insert into usuario(nome, id_departamento) values ('" + Usulist.Nome + "', '" + Usulist.DepartamentoSelecionado + "')";
        SqlCommand cmd = new SqlCommand(query, con);

        con.Open();
        int i = cmd.ExecuteNonQuery();
        con.Close();

        if (i >= 1)
            return true;
        else
            return false;
    }

//FUNÇÃO QUE LISTA OS DEPARTAMENTOS NO DROPDOWN
        public List<UsuarioModel> DepList()
        {
            Conexao();

            List<UsuarioModel> listaDepartamento = new List<UsuarioModel>();

            string query = "select id, nome from departamento";
            SqlCommand cmd = new SqlCommand(query, con);

            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();

            con.Open();
            adapter.Fill(dt);
            con.Close();

            foreach (DataRow dr in dt.Rows)
            {
                listaDepartamento.Add(new UsuarioModel
                {
                    Id = Convert.ToInt32(dr["id"]),
                    Nome = Convert.ToString(dr["nome"])
                });
            }
            return listaDepartamento;
        }
    }
}

The Controller is defined as follows:

using projeto.Models;
using System.Data;
using System.Web.Mvc;

namespace projeto.Controllers
{
    public class UsuarioController : Controller
    {
        [HttpGet]
        public ActionResult Cadastrar()
        {
            UsuarioHandler usuarioHandler = new UsuarioHandler();
            ViewBag.departamentos = new SelectList(usuarioHandler.DepList(), "id", "nome");

            return View();
        }

        [HttpPost]
        public ActionResult Cadastrar(UsuarioModel Usulist)
        {
            if (ModelState.IsValid)
            {
                UsuarioHandler usuariohandler = new UsuarioHandler();

                if (usuariohandler.CadastraUsuario(Usulist))
                {
                    ViewBag.Msg = "<div class='alert alert-success alert-dismissable'><a href = '#' class='close' data-dismiss='alert' aria-label='close'>×</a> Cadastro realizado com sucesso!</div>";
                    ModelState.Clear();
                }
                else
                {
                    ViewBag.Msg = "<div class='alert alert-danger alert-dismissable'><a href = '#' class='close' data-dismiss='alert' aria-label='close'>×</a> Erro ao cadastrar!</div>";
                }
            }
            return View();
        }
    }
}

And lastly,% w / w%

@model projetinho.Models.UsuarioModel 

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="container">
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            <label>Informe o nome:</label>
            @Html.EditorFor(model => model.Nome, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Nome, "", new { @class = "text-danger" })
        </div>

        <div class="form-group">
            <label>Selecione um departamento: </label>
             @Html.DropDownListFor(model => model.DepartamentoSelecionado, ViewBag.departamentos as SelectList, new { @class = "form-control" })
        </div>

        <div class="form-group">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </div>
}
    
asked by anonymous 13.02.2018 / 20:14

3 answers

1

There were two errors in my code that returned errors in the registry. The first one was in the function that inserts in the database:

 public bool CadastraUsuario(UsuarioModel Usulist)
    {
    // código omitido

        string query = "insert into usuario(nome, id_departamento) values ('" + Usulist.Nome + "', '" + Usulist.DepartamentoSelecionado + "')";

    // código omitido
    }

At the end of the string, I was passing the string that selected the department. The correct one is to pass the ID of the selected department. in case it stayed:

 public bool CadastraUsuario(UsuarioModel Usulist)
    {
    // código omitido

        string query = "insert into usuario(nome, id_departamento) values ('" + Usulist.Nome + "', '" + Usulist.Id_Departamento + "')";

    // código omitido
    }

Another error was in ViewBag , within View

  @Html.DropDownListFor(model => model.DepartamentoSelecionado, ViewBag.departamentos as SelectList, new { @class = "form-control" })

I have changed so that model receives Id_Departamento instead of receiving Department listing string ( DepartamentoSelecionado ) and has registered.

    
14.02.2018 / 14:00
0

Friend,

See if this solves your problem:

In your Model, initialize your List.

public UsuarioModel() {
     DepartamentoSelecionado = new List<SelectListItem>();
}
    
13.02.2018 / 22:45
0

Dude, create a private method that populates your viewBags and call in the actionResult Register in the GET and POST verb, the error message appears because even though it succeeds or does not register, the registration view is called again, but this time without the populated viewBags!

A viewBag has the lifetime of 1 request, when you call the registration screen it is populated 1 Request, when a request is made (2 requests) they are no longer exist.

    
14.02.2018 / 17:03