I created a Clientes
table and two from clients that are ClientesTelefone
and ClientesEmail
in my form I am generating email and phone fields dynamically, so I would like to know how I can receive and write these values in the tables , could you help me please ??
ApplicationClient
: here I create the client.
public async Task<ApplicationClient> CreateClientAsync(ApplicationClient client)
{
try
{
var result = await _context.Clientes.AddAsync(client);
if (result.State == EntityState.Added)
{
_context.SaveChanges();
}
return result.Entity;
}
catch (Exception)
{
throw;
}
}
ClientController:
if (ModelState.IsValid)
{
var user = await _userManager.GetUserAsync(User);
if (user == null)
{
throw new ApplicationException($"Não é possível carregar o usuário com o ID '{_userManager.GetUserId(User)}'.");
}
var client = new ApplicationClient {
TipoPessoa = model.TypePerson,Nome = model.Name,
RazaoSocial = model.CompanyName, NomeFantasia = model.FantasyName,
Cpfj = model.Cpfj, CEP = model.CEP, UF = model.UF,
Bairro = model.Neighborhood, Cidade = model.City,
Endereco = model.Address, Numero = model.Number,
Complemento = model.Complement, InfoAdicionais = model.AddInformation,
DataNascimento = model.BirthDate, UsuarioId = user.Id
};
var result = await _clientManager.CreateClientAsync(client);
var clientTelephone = new ApplicationClientTelephone
{
Telefone = model.Telephone
};
var clientEmail = new ApplicationClientEmail
{
Email = model.Email
};
TempData["MensagemSucesso"] = "Cliente cadastrado com sucesso";
return View("Index");
}
ApplicationClient:
public class ApplicationClient
{
[Key]
public Guid Id { get; set; }
public String Nome { get; set; }
public Guid UsuarioId { get; set; }
public virtual ICollection<ApplicationClientTelephone> ClientesTelefone { get; set; }
public virtual ICollection<ApplicationClientEmail> ClientesEmail { get; set; }
[ForeignKey("UsuarioId")]
public virtual ApplicationUser Usuario { get; set; }
}
I have ApplicationClientTelephone
E ApplicationClientEmail
.
I wish that after the customer was added, I would start to generate a chain reaction that would add the customer's phone numbers and emails.
If you can help me, thank you very much.