The whole data inside the dropdown loads in the get request and in the post does not

1

I made an edit function that works with 3 different models, the get loads the data of the id that I selected, but at the time of the post, the integers inside the dropdowns come with the attribute 0.

Here is my code for evaluation:

Controller:

[HttpGet]
public ActionResult EditarFuncionario(string id) {

    int Id = Convert.ToInt32(MyCrypto.Decrypt(id));

    var vm = new FuncionarioVm();
    var vmTel = new TelefoneVm();
    var vmEnd = new EnderecoVm();

    var func = db.Funcionarios.Find(Id);
    var tel = db.Telefones.FirstOrDefault(x => x.AssociadoId == Id);
    var end = db.Enderecos.FirstOrDefault(x => x.AssociadoId == Id);

    vm.CarregarVm(func);
    vmTel.CarregarTelVm(tel);
    vmEnd.CarregarEndVm(end);

    ViewData["Funcionarios"] = vm;
    ViewData["Telefones"] = vmTel;
    ViewData["Enderecos"] = vmEnd;


    ConfigurarViewBagVm(vm, vmTel, vmEnd);

    return View(vm);
}

[HttpPost]
public ActionResult EditarFuncionario(FuncionarioVm item1, TelefoneVm item2, EnderecoVm item3) {

    ConfigurarViewBagVm(item1, item2, item3);

    try {
        if (ModelState.IsValid) {

            //AlterarFuncionario(item1, item2, item3);
            var func = new Funcionario();
            func.AssociadoId = item1.AssociadoId;
            func.Email = item1.Email;
            func.ClassificacaoId = item1.ClassificacaoId;
            func.Nome = item1.Nome;
            func.Rg = item1.Rg;
            func.Cpf = item1.Cpf;
            func.SexoId = item1.SexoId;
            func.DataNascimento = item1.DataNascimento;
            func.NroCtps = item1.NumeroCTPS;
            func.SerieCtps = item1.SerieCTPS;
            func.NroPis = item1.NumeroPis;
            func.DataContratacao = item1.DataContratacao;
            func.CargoId = item1.CargoId;
            func.ContaBancaria = item1.ContaBancaria;
            func.ContaBancariaId = item1.ContaBancariaId;
            func.GroupId = item1.GroupId;
            db.Entry(func).State = EntityState.Modified;

            var tel = new Telefone();
            tel.Ddd = item2.Ddd;
            tel.Numero = item2.NumeroTelefone;
            tel.TipoTelefoneId = item2.TipoTelefoneId;
            db.Entry(tel).State = EntityState.Modified;

            var end = new Endereco();
            end.Rua = item3.Rua;
            end.Bairro = item3.Bairro;
            end.Numero = item3.Numero;
            end.Cep = item3.Cep;
            end.Complemento = item3.Complemento;
            end.TipoEndereco = item3.TipoEndereco;
            end.TipoEnderecoId = item3.TipoEnderecoId;
            end.Cidade = item3.Cidade;
            end.CidadeId = item3.CidadeId;
            db.Entry(end).State = EntityState.Modified;

            db.SaveChanges();
            ViewBag.Mensagem = "Alterado com sucesso!";
        }
    } catch (Exception) {
        ModelState.AddModelError("", "Erro");
    }
    return RedirectToAction("ManterFuncionario");
}

Function that sets the dropdowns:

private void ConfigurarViewBagVm(FuncionarioVm vm, TelefoneVm vmTel, EnderecoVm vmE) {
        ViewBag.SexoId = db.Sexos.Select(item => new SelectListItem { Text = item.Nome, Value = item.SexoId.ToString(), Selected = item.SexoId == vm.SexoId }).OrderBy(item => item.Text).ToList();
        ViewBag.CargoId = db.Cargos.Select(item => new SelectListItem { Text = item.Nome, Value = item.CargoId.ToString(), Selected = item.CargoId == vm.CargoId }).OrderBy(item => item.Text).ToList();
        ViewBag.ClassificacaoId = db.Classificacoes.Select(item => new SelectListItem { Text = item.Nome, Value = item.ClassificacaoId.ToString(), Selected = item.ClassificacaoId == vm.ClassificacaoId }).OrderBy(item => item.Text).ToList();
        ViewBag.BancoId = db.Bancos.Select(item => new SelectListItem { Text = item.Nome, Value = item.BancoId.ToString(), Selected = item.BancoId == vm.BancoId }).OrderBy(item => item.Text).ToList();
        ViewBag.GroupId = db.Groups.Select(item => new SelectListItem { Text = item.GroupName, Value = item.GroupId.ToString(), Selected = item.GroupId == vm.GroupId }).OrderBy(item => item.Text).ToList();
        ViewBag.TipoContaId = db.TipoContas.Select(item => new SelectListItem { Text = item.Tipo, Value = item.TipoContaId.ToString(), Selected = item.TipoContaId == vm.TipoContaId }).OrderBy(item => item.Text).ToList();

        ViewBag.TipoTelefoneId = db.TipoTelefones.Select(item => new SelectListItem { Text = item.Tipo, Value = item.TipoTelefoneId.ToString(), Selected = item.TipoTelefoneId == vm.TipoTelefoneId }).OrderBy(item => item.Text).ToList();

        ViewBag.CidadeId = db.Cidades.Select(item => new SelectListItem { Text = item.Nome, Value = item.CidadeId.ToString(), Selected = item.CidadeId == vmE.CidadeId }).OrderBy(item => item.Text).ToList();
        ViewBag.TipoEnderecoId = db.TipoEnderecos.Select(item => new SelectListItem { Text = item.Tipo, Value = item.TipoEnderecoId.ToString(), Selected = item.TipoEnderecoId == vmE.TipoEnderecoId }).OrderBy(item => item.Text).ToList();

        ViewBag.EstadoId = db.Estados.Select(item => new SelectListItem { Text = item.Nome, Value = item.EstadoId.ToString(), Selected = item.EstadoId == vmE.EstadoId }).OrderBy(item => item).ToList();

    }

@using DigitalFashion.Models.Pessoas
@using DigitalFashion.Util

@{ 
    FuncionarioVm Item1 = ViewData["Funcionarios"] as FuncionarioVm;
    TelefoneVm Item2 = ViewData["Telefones"] as TelefoneVm;
    EnderecoVm Item3 = ViewData["Enderecos"] as EnderecoVm;

}


@{
    ViewBag.Title = "EditarFuncionario";
}
@using (Html.BeginForm("EditarFuncionario", "Pessoa", FormMethod.Post)) {
    @Html.AntiForgeryToken()
    @Html.HiddenFor(x => Item1.AssociadoId)
    @Html.HiddenFor(x => Item2.AssociadoId)
    @Html.HiddenFor(x => Item3.AssociadoId)

    <div class="form-horizontal">
        <section class="content-header">
            <h1>Editando os dados do funcionário: @Item1.Nome</h1>
            <ol class="breadcrumb">
                <li><a href="~/Home"><i class="fa fa-home"></i>Home</a></li>
                <li><a href="~/Home">Pessoas</a></li>
                <li class="active"> Editando funcionário</li>
            </ol>
        </section>
        <br />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="container cadastro-cliente">
            <div class="row">
                <div class="col-sm-12">
                    <!-- CONTENT FIRST TAB -->
                    <div class="tab-content cadastro-cliente" id="nav-tabContent">
                        <div class="tab-pane fade show active" id="nav-dados-pessoais" role="tabpanel" aria-labelledby="nav-dados-pessoais-tab">
                            <div class="form-row">
                                <div class="col-md-3 mb-3">
                                    @Html.LabelFor(model => Item1.Nome, htmlAttributes: new { @class = "control-label col-md-3" })
                                    <div class="col-md-12">
                                        @Html.EditorFor(model => Item1.Nome, new { htmlAttributes = new { @class = "form-control" } })
                                        @Html.ValidationMessageFor(model => Item1.Nome, "", new { @class = "text-danger" })
                                    </div>
                                </div>

                                <div class="col-md-3 mb-3">
                                    @Html.LabelFor(model => Item1.Usuario, htmlAttributes: new { @class = "control-label col-md-3" })
                                    <div class="col-md-12">
                                        @Html.EditorFor(model => Item1.Usuario, new { htmlAttributes = new { @class = "form-control" } })
                                        @Html.ValidationMessageFor(model => Item1.Usuario, "", new { @class = "text-danger" })
                                    </div>
                                </div>

                                <div class="col-md-3 mb-3">
                                    @Html.LabelFor(model => Item1.Senha, htmlAttributes: new { @class = "control-label col-md-3" })
                                    <div class="col-md-12">
                                        @Html.EditorFor(model => Item1.Senha, new { htmlAttributes = new { @class = "form-control" } })
                                        @Html.ValidationMessageFor(model => Item1.Senha, "", new { @class = "text-danger" })
                                    </div>
                                </div>

                                <div class="col-md-3 mb-3">
                                    @Html.LabelFor(model => Item1.Rg, htmlAttributes: new { @class = "control-label col-md-2" })
                                    <div class="col-md-10">
                                        @Html.TextBoxFor(model => Item1.Rg, "", new { @class = "form-control" })
                                        @Html.ValidationMessageFor(model => Item1.Rg, "", new { @class = "text-danger" })
                                    </div>
                                </div>
                                <div class="col-md-3 mb-3">
                                    @Html.LabelFor(model => Item1.Cpf, htmlAttributes: new { @class = "control-label col-md-2" })
                                    <div class="col-md-12">
                                        @Html.TextBoxFor(model => Item1.Cpf, "", new { @class = "form-control" })
                                        @Html.ValidationMessageFor(model => Item1.Cpf, "", new { @class = "text-danger" })
                                    </div>
                                </div>

                                <div class="col-md-3 mb-3">
                                    @Html.LabelFor(model => Item1.DataNascimento, htmlAttributes: new { @class = "control-label col-md-2" })
                                    <div class="col-md-12">
                                        @Html.TextBoxFor(model => Item1.DataNascimento, "dd/MM/yyyy", new { @class = "form-control", @type = "date" })                                       
                                        @Html.ValidationMessageFor(model => Item1.DataNascimento, "", new { @class = "text-danger" })
                                    </div>
                                </div>

                                <div class="col-md-3 mb-3">
                                    @Html.LabelFor(model => Item1.DataContratacao, htmlAttributes: new { @class = "control-label col-md-2" })
                                    <div class="col-md-12">
                                        @Html.TextBoxFor(model => Item1.DataContratacao, "dd/MM/yyyy", new { @class = "form-control", @type = "date" })
                                        @Html.ValidationMessageFor(model => Item1.DataContratacao, "", new { @class = "text-danger" })
                                    </div>
                                </div>

                                <div class="col-md-3 mb-3">
                                    @Html.LabelFor(model => Item1.SexoId, htmlAttributes: new { @class = "control-label col-md-2" })
                                    <div class="col-md-12">
                                        @Html.DropDownList("SexoId", null, null, new { @class = "form-control" })
                                        @Html.ValidationMessageFor(m => Item1.SexoId, "", new { @class = "text-danger" })
                                    </div>
                                </div>

                                <div class="col-md-3 mb-3">
                                    @Html.LabelFor(model => Item1.CargoId, htmlAttributes: new { @class = "control-label col-md-2" })
                                    <div class="col-md-12">
                                        @Html.DropDownList("CargoId", null, null, new { @class = "form-control" })
                                        @Html.ValidationMessageFor(m => Item1.CargoId, "", new { @class = "text-danger" })
                                    </div>
                                </div>

                                <div class="col-md-6 mb-3">
                                    @Html.LabelFor(model => Item1.Email, htmlAttributes: new { @class = "control-label col-md-2" })
                                    <div class="col-md-12">
                                        @Html.EditorFor(model => Item1.Email, new { htmlAttributes = new { @class = "form-control" } })
                                        @Html.ValidationMessageFor(model => Item1.Email, "", new { @class = "text-danger" })
                                    </div>
                                </div>

                                <div class="col-md-6 mb-3">
                                    @Html.LabelFor(model => Item1.NumeroPis, htmlAttributes: new { @class = "control-label col-md-2" })
                                    <div class="col-md-12">
                                        @Html.EditorFor(model => Item1.NumeroPis, new { htmlAttributes = new { @class = "form-control" } })
                                        @Html.ValidationMessageFor(model => Item1.NumeroPis, "", new { @class = "text-danger" })
                                    </div>
                                </div>

                                <div class="col-md-3 mb-3">
                                    @Html.LabelFor(model => Item1.NumeroCTPS, htmlAttributes: new { @class = "control-label col-md-2" })
                                    <div class="col-md-12">
                                        @Html.TextBoxFor(model => Item1.NumeroCTPS, "", new { @class = "form-control" })
                                        @Html.ValidationMessageFor(model => Item1.NumeroCTPS, "", new { @class = "text-danger" })
                                    </div>
                                </div>

                                <div class="col-md-3 mb-3">
                                    @Html.LabelFor(model => Item1.SerieCTPS, htmlAttributes: new { @class = "control-label col-md-2" })
                                    <div class="col-md-12">
                                        @Html.TextBoxFor(model => Item1.SerieCTPS, "", new { @class = "form-control" })
                                        @Html.ValidationMessageFor(model => Item1.SerieCTPS, "", new { @class = "text-danger" })
                                    </div>
                                </div>

                                <div class="col-md-3 mb-3">
                                    @Html.LabelFor(model => Item2.Ddd, htmlAttributes: new { @class = "control-label col-md-2" })
                                    <div class="col-md-12">
                                        @Html.TextBoxFor(model => Item2.Ddd, "", new { @class = "form-control" })
                                        @Html.ValidationMessageFor(model => Item2.Ddd, "", new { @class = "text-danger" })
                                    </div>
                                </div>

                                <div class="col-md-3 mb-3">
                                    @Html.LabelFor(model => Item2.NumeroTelefone, htmlAttributes: new { @class = "control-label col-md-2" })
                                    <div class="col-md-12">
                                        @Html.TextBoxFor(model => Item2.NumeroTelefone, "", new { @class = "form-control" })
                                        @Html.ValidationMessageFor(model => Item2.NumeroTelefone, "", new { @class = "text-danger" })
                                    </div>
                                </div>

                                <div class="col-md-3 mb-3">
                                    @Html.LabelFor(model => Item2.TipoTelefoneId, htmlAttributes: new { @class = "control-label col-md-2" })
                                    <div class="col-md-12">
                                        @Html.DropDownList("TipoTelefoneId", null, null, new { @class = "form-control" })
                                        @Html.ValidationMessageFor(m => Item2.TipoTelefoneId, "", new { @class = "text-danger" })
                                    </div>
                                </div>

                                <div class="col-md-8 mb-3">
                                    @Html.LabelFor(model => Item3.Rua, htmlAttributes: new { @class = "control-label col-md-2" })
                                    <div class="col-md-12">
                                        @Html.EditorFor(model => Item3.Rua, new { htmlAttributes = new { @class = "form-control" } })
                                        @Html.ValidationMessageFor(model => Item3.Rua, "", new { @class = "text-danger" })
                                    </div>
                                </div>

                                <div class="col-md-2 mb-3">
                                    @Html.LabelFor(model => Item3.Numero, htmlAttributes: new { @class = "control-label col-md-2" })
                                    <div class="col-md-12">
                                        @Html.EditorFor(model => Item3.Numero, new { htmlAttributes = new { @class = "form-control" } })
                                        @Html.ValidationMessageFor(model => Item3.Numero, "", new { @class = "text-danger" })
                                    </div>
                                </div>
                                <div class="col-md-2 mb-3">
                                    @Html.LabelFor(model => Item3.Bairro, htmlAttributes: new { @class = "control-label col-md-2" })
                                    <div class="col-md-12">
                                        @Html.EditorFor(model => Item3.Bairro, new { htmlAttributes = new { @class = "form-control" } })
                                        @Html.ValidationMessageFor(model => Item3.Bairro, "", new { @class = "text-danger" })
                                    </div>
                                </div>
                                <div class="col-md-2 mb-3">
                                    @Html.LabelFor(model => Item3.Complemento, htmlAttributes: new { @class = "control-label col-md-2" })
                                    <div class="col-md-12">
                                        @Html.EditorFor(model => Item3.Complemento, new { htmlAttributes = new { @class = "form-control" } })
                                        @Html.ValidationMessageFor(model => Item3.Complemento, "", new { @class = "text-danger" })
                                    </div>
                                </div>
                                <div class="col-md-4 mb-3">
                                    @Html.LabelFor(model => Item3.Cep, htmlAttributes: new { @class = "control-label col-md-2" })
                                    <div class="col-md-12">
                                        @Html.EditorFor(model => Item3.Cep, new { htmlAttributes = new { @class = "form-control" } })
                                        @Html.ValidationMessageFor(model => Item3.Cep, "", new { @class = "text-danger" })
                                    </div>
                                </div>
                                <div class="col-md-3 mb-3">
                                    @Html.LabelFor(model => Item3.TipoEnderecoId, htmlAttributes: new { @class = "control-label col-md-2" })
                                    <div class="col-md-12">
                                        @Html.DropDownList("TipoEnderecoId", null, null, new { @class = "form-control" })
                                        @Html.ValidationMessageFor(m => Item3.TipoEnderecoId, "", new { @class = "text-danger" })
                                    </div>
                                </div>
                                <div class="col-md-3 mb-3">
                                    @Html.LabelFor(model => Item3.CidadeId, htmlAttributes: new { @class = "control-label col-md-2" })
                                    <div class="col-md-12">
                                        @Html.DropDownList("CidadeId", null, null, new { @class = "form-control" })
                                        @Html.ValidationMessageFor(m => Item3.CidadeId, "", new { @class = "text-danger" })
                                    </div>
                                </div>

                                <div class="col-md-3 mb-3">
                                        @Html.LabelFor(model => Item3.EstadoId, htmlAttributes: new { @class = "control-label col-md-2" })
                                        <div class="col-md-12">
                                            @Html.DropDownList("EstadoId", null, null, new { @class = "form-control" })
                                            @Html.ValidationMessageFor(m => Item3.EstadoId, "", new { @class = "text-danger" })
                                        </div>
                                    </div>

                                <div class="col-md-3 mb-3">
                                    @Html.LabelFor(model => Item1.ClassificacaoId, htmlAttributes: new { @class = "control-label col-md-2" })
                                    <div class="col-md-12">
                                        @Html.DropDownList("ClassificacaoId", null, null, new { @class = "form-control" })
                                        @Html.ValidationMessageFor(m => Item1.ClassificacaoId, "", new { @class = "text-danger" })
                                    </div>
                                </div>

                                <div class="col-md-4 mb-3">
                                    @Html.LabelFor(model => Item1.ContaBancaria.Numero, htmlAttributes: new { @class = "control-label col-md-2" })
                                    <div class="col-md-12">
                                        @Html.EditorFor(model => Item1.ContaBancaria.Numero, new { htmlAttributes = new { @class = "form-control" } })
                                        @Html.ValidationMessageFor(model => Item1.ContaBancaria.Numero, "", new { @class = "text-danger" })
                                    </div>
                                </div>

                                <div class="col-md-3 mb-3">
                                    @Html.LabelFor(model => Item1.ContaBancaria.Agencia, htmlAttributes: new { @class = "control-label col-md-2" })
                                    <div class="col-md-12">
                                        @Html.EditorFor(model => Item1.ContaBancaria.Agencia, new { htmlAttributes = new { @class = "form-control" } })
                                        @Html.ValidationMessageFor(model => Item1.ContaBancaria.Agencia, "", new { @class = "text-danger" })
                                    </div>
                                </div>

                                <div class="col-md-3 mb-3">
                                    @Html.LabelFor(model => Item1.BancoId, htmlAttributes: new { @class = "control-label col-md-2" })
                                    <div class="col-md-12">
                                        @Html.DropDownList("BancoId", null, null, new { @class = "form-control" })
                                        @Html.ValidationMessageFor(m => Item1.BancoId, "", new { @class = "text-danger" })
                                    </div>
                                </div>

                                <div class="col-md-3 mb-3">
                                    @Html.LabelFor(model => Item1.TipoContaId, htmlAttributes: new { @class = "control-label col-md-2" })
                                    <div class="col-md-12">
                                        @Html.DropDownList("TipoContaId", null, null, new { @class = "form-control" })
                                        @Html.ValidationMessageFor(m => Item1.TipoContaId, "", new { @class = "text-danger" })
                                    </div>
                                </div>

                                <p>
                                    <div class="col-md-offset-2 col-md-10">
                                        <input type="submit" value="Editar Cliente" class="btn btn-success btn-cadastra-cliente" />
                                    </div>
                                </p>

                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
}

I've tried changing my code anyway, but I just can not seem to get past this step.

    
asked by anonymous 20.08.2018 / 16:34

0 answers