Asp Net Core MVC - Create Multiple Form

1

It's the first time I ask a question, and I'm getting used to the page, so forgive me if I make a mistake.

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

Entity Occurrence (main)

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

Entity Address

    [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; }

Neighborhood Entity

    [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; }

Type Entity

    [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 question is mainly based on the time of recording. Because of what I understand, Entity does everything alone, it already relates the fields to the tables and when writing already put the ID's correctly in the tables, as long as they are correctly addressed in the objects. But when debugging, I see that by passing the Get method, the Address always comes with ID = 0. I tried some things but never passed an ID, which is strange, since it is with the identity field and generates the id alone. Could you give me a light please, thank you very much. hug.

    
asked by anonymous 06.07.2018 / 00:10

1 answer

2

Try to make the following 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.

You are not required to use direct insertion by entityframework, due to performace.

Search for Dapper, performance is much better.

    
10.07.2018 / 00:23