Problems removing items from a List<Viewmodel> when Row is removed using Javascript

Asked

Viewed 47 times

0

I have a contact list that is dynamically loaded using Razor and Javascript to remove lines and reorder field positions. The problem occurs when I click the delete log button and delete all contacts (I send an ajax request to the database and delete them all), on the screen they disappear, but the List on which Binding was made continues to load with null and zero records.

inserir a descrição da imagem aqui

If I save and post, the list is loaded with the zeroed and null items. inserir a descrição da imagem aqui

Why are List records not deleted? Is there a problem with the order of Fields indexes? Is there a way to resolve this?

//CONTROLLER (EXCLUSION USING AJAX)

[HttpPost]
[Route("pessoa-contato/remover-pessoa-contato/{id:int}")]
public void DeletePessoaContato(int id)
{
    _pessoaContatoAppService.Remove(id);
}

COMPLETE SOURCE:

@model Retaguarda.Application.ViewModels.Pessoa.PessoaViewModel
@{
    ViewData["Title"] = "_PessoaContato";
}
<div class="form-horizontal">
    <div class="form-group row">
        <div class="col-md-12">
            <div class="col-md-12" id="div-contatos">

                @if (Model.PessoasContatosViewModel != null)
                {
                    @for (int i = 0; i < Model.PessoasContatosViewModel.Count; i++)
                    {
                        <div class="form-group row align-items-center">
                            <div class="col-md-2">
                                @Html.HiddenFor(model => model.PessoasContatosViewModel[i].Id, new { @class = "hid-id" })
                                @Html.HiddenFor(model => model.PessoasContatosViewModel[i].PessoaId, new { @class = "hid-pessoaId" })                                
                                <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 sel-contatoTipo"></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 txt-contato"></span>
                            </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>
                                <span asp-validation-for="PessoasContatosViewModel[i].Detalhes" class="text-danger txt-detalhes"></span>
                            </div>
                            <div class="col-md-2">
                                <div class="checkbox-custom checkbox-default">
                                    <input type="checkbox" asp-for="PessoasContatosViewModel[i].ContatoPrincipal" class="ckb-contatoPrincipal" autocomplete="off" />
                                    <label asp-for="PessoasContatosViewModel[i].ContatoPrincipal" class="ckb-contatoPrincipal">Contato Principal</label>
                                </div>
                            </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>
                    }

                }

            </div>
        </div>
    </div>
    <div class="form-group row">
        <div class="col-md-12">
            <div class="col-md-2">
                <button id="btn-add-contato" type="button" class="btn btn-icon btn-default btn-outline"><i class="icon wb-plus" aria-hidden="true"></i> Novo Contato</button>
            </div>
        </div>
    </div>
</div>

<script>
        $(function () {

            var qtdContatos = $("#div-contatos input.txt-contato").length;

            $("#btn-add-contato").click(function (e) {
                var novoIndice = qtdContatos;
                e.preventDefault();
                $.get("/pessoa-contato-gerenciar/getPessoaContato/" + novoIndice, function (data) {                    
                    $("#div-contatos").append(data);
                    qtdContatos++;
                });
            });

            $("#div-contatos").on("click", ".btn-remover-contato", function (e) {
                e.preventDefault();
                var id = $(this).attr("data-id");

                if (id)
                    $.post("/pessoa-contato/remover-pessoa-contato/" + id);

                $(this).parent().parent().remove(); //Para remover o Panel e não dar problema, tem que add mais um Parent()
                qtdContatos--;

                $("#div-contatos .row").each(function (indice, elemento) {
                    $(elemento).find(".sel-contatoTipo").attr("name", "PessoasContatosViewModel[" + indice + "].ContatoTipoId");
                    $(elemento).find(".txt-contato").attr("name", "PessoasContatosViewModel[" + indice + "].Contato");
                    $(elemento).find(".ckb-contatoPrincipal").attr("name", "PessoasContatosViewModel[" + indice + "].ContatoPrincipal");
                    $(elemento).find(".txt-detalhes").attr("name", "PessoasContatosViewModel[" + indice + "].Detalhes");


                    $(elemento).find(".hid-id").attr("name", "PessoasContatosViewModel[" + indice + "].Id");
                    $(elemento).find(".hid-pessoaId").attr("name", "PessoasContatosViewModel[" + indice + "].PessoaId");
                });
            });
        });
</script>

@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}

inserir a descrição da imagem aqui

  • I could not understand at what moment these data are null

  • @Leandroangelo, when I add Row by Row I inspect each input and the contents get right and as I remove them they are decremented correctly... I don’t understand why this is happening....:(

  • Adds the example of the rendered html by adding a new one. In the case of the question, you put all the information, except the relevant information to the problem

  • @Leandroangelo, add an image inspecting the first added Row. Index is 0.

  • And you no longer have another set with index 0?

  • For what I have inspected, no. As I add a Row, its index is increased ex: 0, 1, 2, 3.... If I inspect all the Rows, they’re on those orders without repeats

  • Pera... you are adding a new person or just contacts?

  • Within my person I have a personal contact list. I can’t just receive a contact list, because it’s a 1 : N. What I do is I receive the personal WMODEL loaded with the database data and do a for to dynamically create the contacts Robson. And the problem seems to me to be right there, because when removing a Rowling the list continues with the empty items.. when add a new Rowling and Remove seems to work.

Show 3 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.