I am creating some fields dynamically in asp.net core mvc to register a contact. For this, I use a partialview.
Within a div I'll add each row of fields as the user clicks the Add New Contact button.
Through a FOR, I control the index of all contacts to be generated within my vid. Ex:
@for (int i = 0; i < Model.PessoasContatosViewModel.Count; i++)
{
<div class="row align-items-center">
<div class="col-md-2">
@Html.HiddenFor(model => model.PessoasContatosViewModel[i].Id, new { @class = "hid-id" })
<label asp-for="PessoasContatosViewModel[i].ContatoTipoId" class="control-label sel-contatoTipo">Tipo de Contato</label>
<select asp-for="PessoasContatosViewModel[i].ContatoTipoId" asp-items="Model.ContatosTipos" data-plugin="selectpicker" title="Selecione uma opção" class="form-control show-tick show-menu-arrow sel-contatoTipo"></select>
<span asp-validation-for="PessoasContatosViewModel[i].ContatoTipoId" class="text-danger"></span>
</div>
<div class="col-md-4">
<label asp-for="PessoasContatosViewModel[i].Contato" class="control-label txt-contato">Contato</label>
<input type="text" asp-for="PessoasContatosViewModel[i].Contato" class="form-control txt-contato" />
<span asp-validation-for="PessoasContatosViewModel[i].Contato" class="text-danger"></span>
</div>
<div class="col-md-2">
<label class="control-label"> </label>
<div class="checkbox-custom checkbox-default">
<input type="checkbox" asp-for="PessoasContatosViewModel[i].ContatoPrincipal" class="ckb-contatoPrincipal" checked autocomplete="off" />
<label asp-for="PessoasContatosViewModel[i].ContatoPrincipal" class=" ckb-contatoPrincipal">Contato Principal</label>
</div>
</div>
<div class="col-md-3">
<label asp-for="PessoasContatosViewModel[i].Detalhes" class="control-label txt-detalhes">Detalhes</label>
<textarea asp-for="PessoasContatosViewModel[i].Detalhes" class="form-control txt-detalhes"></textarea>
</div>
<div class="col-md-1">
<button type="button" class="btn btn-icon btn-default btn-outline btn-remover-contato" data-id="@Model.PessoasContatosViewModel[i].Id." style="margin-top: 30px;"><i class="icon wb-trash" aria-hidden="true"></i></button>
</div>
</div>
}
The Problem:
My PartialView is linked to the ViewModel PersonContactViewModel where I need to index the fields to be returned to the main View along with the others. For this I am passing the index number through a ViewBag, but I do not know how to create the indexes. It would look something like this:
<div class="col-md-4">
<label asp-for="[indice]Contato" class="control-label txt-contato">Contato</label>
<input type="text" asp-for="[indice]Contato" class="form-control txt-contato" />
<span asp-validation-for="[indice]Contato" class="text-danger"></span>
</div>
@model Retaguarda.Application.ViewModels.Pessoa.PessoaContatoViewModel
@{
int indice = ViewBag.indice;
}
<div class="row align-items-center">
<div class="col-md-2">
@Html.HiddenFor(model => model.Id, new { @class = "hid-id" })
<label asp-for="ContatoTipoId" class="control-label sel-contatoTipo">Tipo de Contato</label>
<select asp-for="ContatoTipoId" asp-items="Model.ContatosTipos" data-plugin="selectpicker" title="Selecione uma opção" class="form-control show-tick show-menu-arrow sel-contatoTipo"></select>
<span asp-validation-for="ContatoTipoId" class="text-danger"></span>
</div>
<div class="col-md-4">
<label asp-for="Contato" class="control-label txt-contato">Contato</label>
<input type="text" asp-for="Contato" class="form-control txt-contato" />
<span asp-validation-for="Contato" class="text-danger"></span>
</div>
<div class="col-md-2">
<label class="control-label"> </label>
<div class="checkbox-custom checkbox-default">
<input type="checkbox" asp-for="ContatoPrincipal" class="ckb-contatoPrincipal" checked autocomplete="off" />
<label asp-for="ContatoPrincipal" class=" ckb-contatoPrincipal">Contato Principal</label>
</div>
</div>
<div class="col-md-3">
<label asp-for="Detalhes" class="control-label txt-detalhes">Detalhes</label>
<textarea asp-for="Detalhes" class="form-control txt-detalhes"></textarea>
<span asp-validation-for="Detalhes" class="text-danger"></span>
</div>
<div class="col-md-1">
<button type="button" class="btn btn-icon btn-default btn-outline btn-remover-contato" data-id="@Model.Id." style="margin-top: 30px;"><i class="icon wb-trash" aria-hidden="true"></i></button>
</div>
</div>
<script src="~/lib/jquery-validation/dist/jquery.validate.js"></script>
<script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js"></script>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
How do I do this?