I have a people register and I need to create a button to add multiple phones and send to my controller, I'm using BeginCollectionItem, based on my other question Form using BeginCollectionItem gets null viewModel in foreach just missing the button part, how do I?
My controller:
// GET: Pessoas/Create
public ActionResult Create()
{
var pessoaViewModel = new PessoaViewModel
{
PessoaTelefoneViewModel = new List<PessoaTelefoneViewModel>
{
new PessoaTelefoneViewModel()
}
};
return View(pessoaViewModel);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(PessoaViewModel pessoaViewModel)
{
//salvando os dados
return RedirectToAction("Index");
}
My view:
@model meuprojeto.ViewModels.PessoaViewModel
// resto da view...
<div id="tab-3" class="tab-pane">
@if (Model != null && Model.PessoaTelefoneViewModel != null)
{
foreach (var telefone in Model.PessoaTelefoneViewModel)
{
Html.RenderPartial("_Telefone",telefone);
}
}
</div>
My partial phone view:
@model meuprojeto.ViewModels.PessoaTelefoneViewModel
@using (Html.BeginCollectionItem("PessoaTelefoneViewModel"))
{
<div class="form-group">
@Html.LabelFor(model => model.Descricao, new {@class = "col-md-12 "})
<div class="col-md-10">
@Html.EditorFor(model => model.Descricao, new {htmlAttributes = new {@class = "form-control "}})
@Html.ValidationMessageFor(model => model.Descricao, "", new {@class = "text-danger"})
</div>
</div>
//outros campos...
}