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">×</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();
}
In this example you need to give the post to the server, no?
– Fabri Damazio
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?
– João
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/
– Fabri Damazio