Does not delete record from list and SQL database

Asked

Viewed 85 times

0

I would like you to delete the desired record not only from the list but from the bank as well, how can I proceed? He’s going with the id "0" Instead of taking the id of the record I wish to delete.

inserir a descrição da imagem aqui

This is my list... inserir a descrição da imagem aqui

//NA CONTROLLER...

[HttpPost]
public async Task < JsonResult > Excluir(int IdLogIncluirCep) {

  if (IdLogIncluirCep > 0) {

    bool resultado = await Task.Factory.StartNew(() => {
      return new LogIncluirCepBLL().ExcluirLoginIncluirCep(IdLogIncluirCep);

    });

    if (resultado) {
      return Json(new {
        Execucao = "Sucesso", Mensagem = "CEP excluído com sucesso!"
      });
    }
  }

  return Json(new {
    Execucao = "Erro", Mensagem = "Houve um erro ao tentar excluir o CEP. Tente novamente."
  });
}

//NO HTML...

<div class="box">
  <div class="box-head">
    <h3 class="box-title">LogCep</h3>
    <div class="msg  w3-margin-top"></div>
  </div>
  <div class="box-content table-responsive no-padding">
    @if (Model != null) { if (Model.logCeps.Count > 0) {
    <table class="table table-hover">
      <thead>
        <tr class="w3-light-grey">
          <!--<th>Id</th>-->
          <th style="width:6%">Id LogCep.</th>
          <th>Cep</th>
          <th class="">Data de Criação</th>
          <th class="text-center tb-action">Incluir</th>
          <th class="text-center tb-action">Excluir</th>
        </tr>
      </thead>

      @foreach (var item in Model.logCeps) {
      <tr>
        <td>@item.IdLogIncluirCep</td>
        <td>@item.DsCep</td>
        <td>@item.DtPesquisa.ToString("dd/MM/yyyy HH:mm:ss")</td>
        <td class="col-editar"><a href="@Url.Action(" Cadastrar ","Cep ")[email protected]&[email protected]"><span class="ti-pencil" aria-hidden="true"></span></a></td>
        <td class="col-excluir"><span class="ti-trash" aria-hidden="true"></span></td>
      </tr>
      }
    </table>
    } }

  </div>

  <div class="modal fade" id="confirm" role="dialog">
    <div class="modal-dialog modal-md">
      <div class="modal-content">
        <div class="modal-body">
          <h5>
            <p>Confirma a exclusão do CEP?</p>
          </h5>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-danger" id="delete" value="Excluir" onclick="ExcluirLoginIncluirCep()">Apagar CEP</button>
          <button type="button" data-dismiss="modal" class="btn btn-default">Cancelar</button>
        </div>
      </div>

    </div>
  </div>
</div>

<script type="text/javascript">
  $('.ti-trash').click(function() {
    $("#confirm").modal();
  })


  function ExcluirLoginIncluirCep(IdLogIncluirCep) {
    var sucesso = true;
    var IdLogIncluirCep = 0;
    $.ajax({
      type: 'POST',
      url: '@Url.Action("Excluir","Cep")',
      async: false,
      contentType: 'application/json; charset=utf-8',
      dataType: 'json',
      data: JSON.stringify({
        'IdLogIncluirCep': 0 + IdLogIncluirCep
      }),
      success: function(data) {
        if (data.Execucao != 'Sucesso') {
          sucesso = false;
        }
      },
      error: function(data) {
        sucesso = false;
      }

    });
    return sucesso;
  }
</script>

//NO REPOSITÓRIO
 
 public bool ExcluirLoginIncluirCep(int IdLogIncluirCep)
        {
            try
            {
                var logcep = banco.LogIncluirCep.Where(x => x.IdLogIncluirCep == IdLogIncluirCep).First().FgCadastrado=true;
                banco.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                new Logger.Logger().FazerLogAsync(ex, new { Tipo = "Erro", Mensagem = "Erro ao excluir CEP" }, Logger.EnumTiposDeLog.TiposDeLog.Erro);
                return false;
            }           
        }

  • You always arrow id as 0... var IdLogIncluirCep = 0;

  • @Leandroangelo tried to change it, but it didn’t happen... :(

  • 1

    @Rpark Here’s another problem, you’re not capturing the value of the Id, but you can be sure that while you’re assigning the 0 he will arrive 0 in controller

  • @Leandroangelo In case I had to capture his ID, that’s what I was trying to do... But I didn’t get very clear.. I’ll post the method I tried to capture...

  • 1

    you are not passing the id in onclick="Excluirloginincluircep()"

  • 2

    try trading for onclick="Exclude Loginincluircep(@item.Idlogincluircep)" and remove this line var Idlogincluircep = 0;

Show 1 more comment
No answers

Browser other questions tagged

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