When field validation is triggered, dropdownlists do not load in Asp.net core MVC

0

When I start a registration screen, the system loads ViewModel with combobox loaded:

  

IfI,forexample,clickthesavebutton,thefieldsinmyviewmodelarevalidated,butthepagedoesnotkeepthecomboboxloaded.

Howtosolvethis?

  

publicclassPessoaFisicaViewModel{[DisplayName("Código")]
        public int Id { get; set; }

        [Required(ErrorMessage ="Campo obrigatório")]
        [DisplayName("Nome Completo")]
        public string NomeCompleto { get; set; }

        [DisplayName("Apelido")]
        public string Apelido { get; set; }

        [Required(ErrorMessage = "Campo obrigatório")]
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd}")]
        [DataType(DataType.Date, ErrorMessage = "Data em formato inválido")]
        [DisplayName("Nascimento")]
        public DateTime? DataNascimento { get; set; }


        [DisplayName("Sexo")]
        public Sexo Sexo { get; set; }
        [DisplayName("Sexo")]
        public string SexoDescricao { get; set; }
        [DisplayName("Sexos")]
        public IEnumerable<SelectListItem> Sexos { get; set; }


        [DisplayName("Estado Civil")]
        public EstadoCivil EstadoCivil { get; set; }
        [DisplayName("Estado Civil")]
        public string EstadoCivilDescricao { get; set; }
        [DisplayName("Estados Civis")]
        public IEnumerable<SelectListItem> EstadosCivis { get; set; }

}
@model SistemaComercial.Application.ViewModels.Pessoa.PessoaViewModel
@{
    ViewData["Title"] = "PessoaFisica";
}

<div class="form-horizontal">
    <div class="form-group">
        <label asp-for="PessoaFisicaViewModel.NomeCompleto" class="col-md-2 control-label"></label>
        <div class="col-md-8">
            <input asp-for="PessoaFisicaViewModel.NomeCompleto" class="form-control" />
            <span asp-validation-for="PessoaFisicaViewModel.NomeCompleto" class="text-danger"></span>
        </div>
    </div>
    <div class="form-group">
        <label asp-for="PessoaFisicaViewModel.Apelido" class="col-md-2 control-label"></label>
        <div class="col-md-8">
            <input asp-for="PessoaFisicaViewModel.Apelido" class="form-control" />
            <span asp-validation-for="PessoaFisicaViewModel.Apelido" class="text-danger"></span>
        </div>
    </div>
    <div class="form-group">
        <label asp-for="PessoaFisicaViewModel.DataNascimento" class="col-md-2 control-label"></label>
        <div class="col-md-4">
            <input asp-for="PessoaFisicaViewModel.DataNascimento" class="form-control" />
            <span asp-validation-for="PessoaFisicaViewModel.DataNascimento" class="text-danger"></span>
        </div>
    </div>
    <div class="form-group">
        <label asp-for="PessoaFisicaViewModel.Sexo" class="col-md-2 control-label"></label>
        <div class="col-md-2">
            <select asp-for="PessoaFisicaViewModel.Sexo" asp-items="Model.PessoaFisicaViewModel.Sexos" class="form-control">
                <option value="">--Selecione--</option>
            </select>
            <span asp-validation-for="PessoaFisicaViewModel.Sexo" class="text-danger"></span>
        </div>
    </div>
    <div class="form-group">
        <label asp-for="PessoaFisicaViewModel.EstadoCivil" class="col-md-2 control-label"></label>
        <div class="col-md-3">
            <select asp-for="PessoaFisicaViewModel.EstadoCivil" asp-items="Model.PessoaFisicaViewModel.EstadosCivis" class="form-control">
                <option value="">--Selecione--</option>
            </select>
            <span asp-validation-for="PessoaFisicaViewModel.EstadoCivil" class="text-danger"></span>
        </div>
    </div>
</div>

[HttpPost]
        [Authorize(Policy = "CanWriteCustomerData")]
        [Route("pessoa-gerenciamento/cadastrar-novo")]
        [ValidateAntiForgeryToken]
        public IActionResult Create(PessoaViewModel pessoaViewModel)
        {
            if (!ModelState.IsValid) return View(pessoaViewModel);
            _pessoaAppService.Register(pessoaViewModel);

            if (IsValidOperation())
                ViewBag.Sucesso = "Pessoa Cadastrada!";

            return View(pessoaViewModel);
        }

    
asked by anonymous 05.03.2018 / 02:31

2 answers

1

Look closely at your code, I finally noticed that the object passed is PessoaFisicaViewModel , and the View is signed as PessoaViewModel , apparently PessoaViewModel does not have the combobox listing properties (the ICollection), for when they are submitted, the conversion from PessoaFisicaViewModel to PessoaViewModel causes these properties to be lost.

    
05.03.2018 / 17:40
0

In this case, I get the impression that you select the values in the Combos sends to SUBMIT, but as nothing is still saved, the validation is done but the object is returned again by _pessoaAppService.GetJoinById() and in this case Combo values they do not exist in the object returned by this service, and therefore do not return populates.

In my understanding, what you should do is, in validation, return the validated object and not replace it with the object returned by _pessoaAppService.GetJoinById() .

    
05.03.2018 / 13:40