Creating a modal with delete function . Net Core 2.0

Asked

Viewed 449 times

1

Good evening, I would like help on a question of . Net Core 2.0, how can I create a modal of boostrap 4 with the delete function in the code below? My main problem is on how to recover and make the modal delete button erase the record from the table. My cshtml index:

   @{
    ViewData["Title"] = "Chamados";
}

<div class="container">
        <div class="table-wrapper">
            <div class="table-title">
                <div class="row">
                    <div class="col-sm-6">
                        <h2>Lista de Chamados</b></h2>
                    </div>
                    <div class="col-sm-6">
                        <a href="~/Chamados/Cadastro" class="btn btn-success"><i class="fas fa-plus-circle"></i> 
            <span>Novo Chamado</span></a>

                    </div>
                </div>
            </div>
            <table class="table table-striped table-hover">
                <thead>
                    <tr>
                 <th>ID</th>
                 <th>Titulo</th>
                 <th>Status</th>
                 <th>Valor</th>
                 <th>Ações</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>                        
            @{
              foreach (var item in (List<ChamadosModel>)ViewBag.ListaChamados){
              <tr>          
              <td>@item.Id</td>
              <td>@item.Titulo</td>
              <td>@item.Status</td>
              <td>@item.Valor</td>
              <td>
            <a href="#editEmployeeModal" class="edit" data-toggle="modal" onclick="Editar(@item.Id)"><i class="fas fa-edit" data-toggle="tooltip" title="Edit"></i></a>

              <a href="#deleteEmployeeModal" class="delete" data-toggle="modal" data-id="@item.Id"><i class="fas fa-trash" data-toggle="tooltip" title="Delete"></i></a>
              </td>
        </tr>
        }
    }
                        </td>
                    </tr>

                </tbody>
            </table>


    <!-- Delete Modal HTML -->
    <div id="deleteEmployeeModal" class="modal fade">
        <div class="modal-dialog">
            <div class="modal-content">
                <form>
                    <div class="modal-header">                      
                        <h4 class="modal-title">Deletar Chamado</h4>
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    </div>
                    <div class="modal-body">                    
                        <p>Tem certeza que deseja deletar este chamado?</p>
                        <p class="text-warning"><small>Está ação não tem volta!</small></p>
                    </div>
                    <div class="modal-footer">
                         @using (Html.BeginForm())
                {
                    @Html.AntiForgeryToken()
                    <div class="form-actions no-color">
                        <button type="button" class="btn btn-danger" data-dismiss="modal">Cancelar</button>
                        <input type="submit" value="Delete" class="btn btn-primary" />                     
                    </div>
                }
                    </div>
                </form>
            </div>
        </div>
    </div>

 <script>
        $(function () {

            $(".delete").click(function () {
                var id = $(this).attr("data-id");

                $("#modal").load("Delete?id=" + id, function () {
                    $("#modal").modal();
                })
            });

        })
    </script>

Model:

 public void Delete(int id)
        {
            DAL objDal = new DAL();
            string sql = $"DELETE FROM CHAMADOS WHERE ID='{id}'";

            objDal.ExecutarComandoSQL(sql);
        }

Controller:

 [HttpPost]
         public IActionResult Delete(int id){
             new ChamadosModel().Delete(id);
             return View();
         }
  • 1

    In this example you need to give the post to the server, no?

  • In my Iactionresult I’m not doing iso? I’m still starting to study Net Core, if not so could give me an example of how to do?

  • 1

    Your controller receives this ajax. You have to do it from your javascript to it. Here you can see the post documentation of Jquery https://api.jquery.com/jquery.post/

1 answer

2


You can do without using ajax as below

View Index

<a href="#deleteEmployeeModal" class="delete" data-toggle="modal" data-id="@item.Id"><i class="fas fa-trash" data-toggle="tooltip" title="Delete"></i></a>

<!-- Delete Modal HTML -->
<div id="deleteEmployeeModal" class="modal fade">
    <div class="modal-dialog">
        <div class="modal-content">            
                <div class="modal-header">                      
                    <h4 class="modal-title">Deletar Chamado</h4>
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                </div>
                <div class="modal-body">                    
                    <p>Tem certeza que deseja deletar este chamado?</p>
                    <p class="text-warning"><small>Está ação não tem volta!</small></p>
                </div>
                <div class="modal-footer">
                     
          
                <form asp-action="Delete">
                <div class="form-actions no-color">
                    <button type="button" class="btn btn-danger" data-dismiss="modal">Cancelar</button>
                    <button type="submit" value="Delete" class="btn btn-primary" />                     
                </div>
            </form>
        </div>
    </div>
</div>

Just direct the form post to your "Delete" action in the controller.

  • It is directing to the controller, but not excluding the calls, I have to make some other modification in the controller or model I posted?

  • 1

    You’re giving the Savechanges()?

  • No, I’m starting studies yet, as I use in the context of my application?

  • 1

    So... in your controller you should: public Iactionresult(int id) { new Calledsmodel.Delete(id); Calledsmodel.Savechanges(); Return View(); } .

Browser other questions tagged

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