Viewmodel is NULL when performed a Submit in the Main View - Asp.Net Core MVC

Asked

Viewed 81 times

0

I have a Viewmodel (FilialViewModel) who owns a property of the type PessoaViewModel. The main view is linked to the Filialviewmodel and whenever I make a Submit, the property of the Personal type is NULL. This is because, at the moment I call Partialview Pessoa, I pass Filialviewmodel.PessoaViewModel... When performing Ubmit, only the Filialviewmodel fields are loaded... Does anyone know how to solve this?

public class FilialViewModel
{
    [Key]
    public int PessoaFilialId { get; set; }

    [DisplayName("Tipo de Filial")]
    [Required(ErrorMessage = "Escolha um Tipo de Filial")]
    public FilialTipo FilialTipo { get; set; }

    public PessoaViewModel PessoaViewModel { get; set; }

}

public class PessoaViewModel
{
    [Key]
    public int Id { get; set; }

    [DisplayName("Natureza")]
    [Required(ErrorMessage = "Escolha uma Natureza")]
    public PessoaNatureza PessoaNatureza { get; set; }
    [DisplayName("Natureza")]
}

View Filial:

@model Retaguarda.Application.ViewModels.Filial.FilialViewModel
@{

}

<div class="panel">
    <div class="panel-body container-fluid pt-5 pl-15 pr-15">
        <div class="form-horizontal">
            <vc:summary />
           @await Html.PartialAsync("~/Views/Pessoa/_Pessoa.cshtml", Model.PessoaViewModel)
        </div>
    </div>
</div>

View Pessoa:

@model Retaguarda.Application.ViewModels.Pessoa.PessoaViewModel
@using Retaguarda.Domain.Enuns.Pessoa
@{
    ViewData["Title"] = "Pessoa";
}

@Html.HiddenFor(model => model.Id, new { @class = "hidden-id" })
<div class="col-md-2">
    <label asp-for="PessoaNatureza" class="control-label">Natureza</label>
    <select id="pessoaNatureza" data-modal="" asp-for="PessoaNatureza" asp-items="Model.PessoasNaturezas" title="Selecione uma opção" class="form-control"><option value=""></option></select>
    <span asp-validation-for="PessoaNatureza" class="text-danger"></span>
</div>

Controller:

[HttpPost]
[Authorize(Policy = "CanWriteFilialData")]
[Route("filial-gerenciar/cadastrar-novo")]
[ValidateAntiForgeryToken]
public IActionResult Create(FilialViewModel filialViewModel)
{
    if (!ModelState.IsValid) return View(filialViewModel);
    _filialAppService.Register(filialViewModel);

    if (IsValidOperation())
        ViewBag.Sucesso = "Filial cadastrada!";
    // return Json(new { success = true, message = "Pessoa Excuída!" });
    return View(filialViewModel);

}

inserir a descrição da imagem aqui

1 answer

1


PessoaViewModel comes null because at no time she was instantiated.

You can solve this by instantiating in a builder:

public class FilialViewModel
{
    public FilialViewModel()
    {
        PessoaViewModel = new PessoaViewModel();
    }

    [Key]
    public int PessoaFilialId { get; set; }

    [DisplayName("Tipo de Filial")]
    [Required(ErrorMessage = "Escolha um Tipo de Filial")]
    public FilialTipo FilialTipo { get; set; }

    public PessoaViewModel PessoaViewModel { get; set; }

}
  • Thank you @George Wurthmann!

Browser other questions tagged

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