I have a modal box that should confirm the deletion of some registers. When I click the "Excluir" button the modal appears as it should but when I click "Confirma" in the modal I get the following error:
Server Error in '/' Application. The parameters dictionary contains a null entry for parameter 'id' of non-nullable type
System.Int32
for methodSystem.Web.Mvc.ActionResult Delete(Int32)
inNFE.Web.Controllers.NaturezaController
. An optional parameter must be a reference type, anullable
type, or be declared as an optional parameter. Parameter name: parameters
Here's the JQuery code that appends the modal to the html file:
function ModalExclusao(ID) {
var htmlModal = '';
htmlModal += '<div class="modal fade modalExclusao" id="modalExclusao" tabindex="-1" role="dialog" aria-labelledby="modalExclusaoLabel">';
htmlModal += '<div class="modal-dialog" role="document">';
htmlModal += '<div class="modal-content">';
htmlModal += '<div class="modal-header">';
htmlModal += '<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>';
htmlModal += '<h4 class="modal-title" id="modalExclusao">Confirmação de exclusão</h4>';
htmlModal += '</div>';
htmlModal += '<h5 style="text-align: center;">Tem certeza que deseja excluir permanentemente o registro?</h5>';
htmlModal += '<form method="POST" action="Delete/">';
htmlModal += '<div class="modal-footer">';
htmlModal += '<input type="text" id="'+ ID +'" name="'+ ID +'" hidden/>';
htmlModal += '<button type="button" class="btn btn-danger" data-dismiss="modal">Cancelar</button>';
htmlModal += '<button type="submit" class="btn btn-success">Confirmar</button>';
htmlModal += '</div>';
htmlModal += '</form>';
htmlModal += '</div>';
htmlModal += '</div>';
htmlModal += '</div>';
$('#modal').html(htmlModal);}
That's the table with the registers:
<table class="table table-condensed table-bordered table-hover" id="DataTables">
<thead>
<tr>
<th> Nome </th>
<th> CFOP Dentro do estado </th>
<th> CFOP Fora do estado </th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@item.Nome
</td>
<td>
@item.CFPODentroEstado
</td>
<td>
@item.CFPOForaEstado
</td>
<td>
<a href="/Natureza/Edit/@item.IDNatureza">Edit</a> |
<a href="javascript:func()" onclick="ModalExclusao(@item.IDNatureza)" data-toggle="modal" data-target=".modalExclusao">Excluir</a>
</td>
</tr>
}
<div id="modal"></div>
</tbody>
</table>
And here's the Object controller:
// POST: Natureza/Delete/5
[HttpPost]
public ActionResult Delete(int id)
{
try
{
if (VerificaSessao((List<Natureza>)Session["ListaDeNatureza"]))
{
List<Natureza> naturezas = new List<Natureza>();
naturezas = (List<Natureza>)Session["ListaDeNatureza"];
foreach (Natureza natureza in naturezas)
{
if (natureza.IDNatureza == id)
{
naturezas.Remove(natureza);
return RedirectToAction("Index", "Natureza");
}
}
}
// Se não tiver encontrado natureza retornar uma mensagem
return RedirectToAction("Index", "Natureza");
}
catch
{
return View();
}
}