How to update the Index of a field using JavaScript in Asp.net MVC

0

I am creating fields dynamically based on a List. When I remove a field, I need to reorder them using javascript, but I'm facing problems since all fields are reordered in the Indexes, except those using the "asp-validation-for" razor ... Would I have a Find and change his Index too?

Razor:

<div class="col-md-2">
    <label asp-for="PessoaFisicaViewModel.PessoasFisicasDocumentosViewModel[i].Documento" class="control-label txt-documento">Documento</label>
    <input type="text" asp-for="PessoaFisicaViewModel.PessoasFisicasDocumentosViewModel[i].Documento" class="form-control txt-documento" />
    <span asp-validation-for="PessoaFisicaViewModel.PessoasFisicasDocumentosViewModel[i].Documento" class="text-danger txt-documento"></span>
</div>

JavaScript:

$(this).parent().parent().remove();
qtdDocumentos--;
$("#div-documentos .row").each(function (indice, elemento) {
    $(elemento).find(".txt-documento").attr("name", "PessoaFisicaViewModel.PessoasFisicasDocumentosViewModel[" + indice + "].Documento");
});

    
asked by anonymous 17.10.2018 / 19:55

1 answer

0

I thought about getting all the elements of page validation and for each one to change the% of them% according to the index.

So if you have other validation fields on the screen you will have to optimize the search for the components directly from some div.

Maybe this will help or at least give you a north to follow friend:

  function ReordenarValidation()
  {
    var elements = document.getElementsByClassName('text-danger');

    var i;
    for (i = 0; i < elements.length; i++) {
      elements[i].attr("name", "PessoaFisicaViewModel.PessoasFisicasDocumentosViewModel["+ i +"].Documento");
    }
  }
    
18.10.2018 / 12:30