Asp Net Core MVC - Create Form with Multiple

Asked

Viewed 177 times

1

It’s the first time I’ve asked a question, and I’m getting used to the page, so forgive me if I miss some rule.

I’m new to Asp Net Core MVC and I’m picking up a lot to understand some things. The one that is preventing me from continuing my work in the way I would like, I believe it is time to link the tables created to the main record.

Occurrence Entity (main)

    [Key]
    [Display(Name = "ID da Ocorrência")]
    public int OcorrenciaID { get; set; }
    public int EnderecoID { get; set; }
    public Endereco Endereco { get; set; }

Entidade Endereco

    [Key]
    [Display(Name = "ID do Endereço")]
    public int EnderecoID { get; set; }
    [Display(Name = "Logradouro")]
    [Required(ErrorMessage = "Campo Obrigatório!")]
    public string Logradouro { get; set; }

    [Display(Name = "Tipo")]
    [Required(ErrorMessage = "Campo Obrigatório!")]
    public int TipoID { get; set; }
    [Display(Name = "Tipo")]
    public Tipo Tipo { get; set; }

    [Display(Name = "Bairro")]
    [Required(ErrorMessage = "Campo Obrigatório!")]
    public int BairroID { get; set; }
    [Display(Name = "Bairro")]
    public Bairro Bairro { get; set; }

    [Display(Name = "Ocorrências")]
    public virtual ICollection<Ocorrencia> Ocorrencias { get; set; }

Entity Bairro

    [Key]
    [Display(Name = "ID do Bairro")]
    public int BairroID { get; set; }

    [Display(Name = "Bairro")]
    [Required(ErrorMessage = "Campo Obrigatório!")]
    [StringLength(50, MinimumLength = 5, ErrorMessage = "Digite entre 5 e 50 caracteres!")]
    public string DescBairro { get; set; }

    [Display(Name = "Cidade")]
    public int CidadeID { get; set; }
    [Display(Name = "Cidade")]
    public Cidade Cidade { get; set; }

    [Display(Name = "Endereços")]
    public ICollection<Endereco> Enderecos { get; set; }

Entity Type

    [Key]
    [Display(Name = "ID do Tipo")]
    public int TipoID { get; set; }

    [Display(Name = "Tipo")]
    [Required(ErrorMessage = "Campo Obrigatório!")]
    public string DescTipo { get; set; }

    [Display(Name = "Selecionado")]
    public bool Assinado { get; set; }

    [Display(Name = "Descrição")]
    public string DescOutros { get; set; }

    [Display(Name = "Endereços")]
    public ICollection<Endereco> Enderecos { get; set; }

CONTROLLER

public Iactionresult Create()

    {            
        PopularBairroDDL();
        PopularTiposDDL();
        return View();
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Create([Bind("OcorrenciaID,NumOcorrencia,NumGerado,EnderecoID,BairroID,TipoID")]
    Ocorrencia ocorrencia, Endereco endereco)
    {
        Endereco end = new Endereco
        {
            BairroID = endereco.BairroID,
            TipoID = endereco.TipoID
        };
        ocorrencia.Endereco = end;
        ocorrencia.EnderecoID = end.EnderecoID;

        if (ModelState.IsValid)
        {
            _context.Add(ocorrencia);
            await _context.SaveChangesAsync();
            return RedirectToAction(nameof(Index));
        }
        PopularBairroDDL(ocorrencia);
        PopularTiposDDL(ocorrencia);
        return View(ocorrencia);
    }

My doubt is mainly based on the timing of the recording. Because from what I understand, the Entity does everything alone, it already relates the fields to the tables and when recording already put the ID’s correctly in the tables, since directed correctly in the objects. But when debugging, I see that when going through the Get method, Endereco always comes with ID = 0. I tried some things but never passes an ID, which is strange, since it is with the Identity field and generates the id alone. Could you give me a light please, I’d really appreciate it. hug.

  • Some information is missing to understand the flow your application has, but try to mark the Address property of the Occurrence class as virtual

1 answer

1

Try to make the next change

  Endereco end = new Endereco
    {
        BairroID = endereco.BairroID,
        TipoID = endereco.TipoID
    };
    ocorrencia.Endereco = end;
    ocorrencia.EnderecoID = end.EnderecoID;

for

    Endereco end = new Endereco
    {
        BairroID = endereco.BairroID,
        TipoID = endereco.TipoID
    };
    _context.Add(end);
    await _context.SaveChangesAsync();
    ocorrencia.Endereco = end;
    ocorrencia.EnderecoID = end.EnderecoID;

This happened in versions in ASP.NET MVC but in . NET CORE this does not happen.

It is not indicated you use direct insertion by entityframework, due to performace.

Search for Dapper, the performance is much better.

Browser other questions tagged

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