I have two Client and Address entities where the client has multiple addresses. At the time of filling out the form, the user can add two addresses in the same form and at the time of the Submit, would like to receive the data of the client and more than one address filled in the same form. How can I receive on my Controller the Customer filled in with more than one address with just a Submit?
In my MVC project I'm following the DDD architecture strategy where my Application layer contains my ViewsModel classes with the ClientEnderecoViewModel, ClientViewModel, and EnderecoViewModel classes where I talk to my ClientController Presentation layer.
My ClientEnderecoViewModel has the following prop below
public Guid ClienteId { get; set; }
public string Nome { get; set; }
public string Email { get; set; }
public string CPF { get; set; }
public ICollection<EnderecoViewModel> ListEnderecoViewModels { get; set; }
My ClientController
public ActionResult Create( ClienteEnderecoViewModel clienteEnderecoViewModel)
{
if (ModelState.IsValid)
{
_clienteAppService.Adicionar(clienteEnderecoViewModel);
return RedirectToAction("Index");
}
return View(clienteEnderecoViewModel);
}
My Create.CSHTML
<h4>Endereco </h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Logradouro, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ListEnderecoViewModels, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Logradouro, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Numero, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Numero, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Numero, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Complemento, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Complemento, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Complemento, "", new { @class = "text-danger" })
</div>
</div>