I've been trying to implement a delete button in my code to remove certain entries from a model. However, I keep encountering an error that I can't seem to troubleshoot. My controller includes an IActionResult called `deletelname`, and everything appears to be logically sound, but the functionality is not working as expected. I would greatly appreciate it if someone could review the code and provide some feedback.
Here's how the view looks:
@model IEnumerable<Models.pinfo>
@using Models
@foreach (var s in Model){
<h1> @s.fname @s.lname </h1> <h3> @s.comment </h3> <a class="GoDelete" href="javascript:void(0)" data-id="@s.lname">Delete</a>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Deleting....</h4>
</div>
<div class="modal-body">
Are you sure you want to Delete this Entry?
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button id="btndelete" type="button" class="btn btn-primary">Delete</button>
</div>
</div>
</div>
</div>
}
@section scripts
{
<script>
$(document).ready(function() {
$("#btndelete").click(function () {
$('#myModal').modal('hide');
var id = $('#hfId').val();
window.location.href = '@Url.Action("deletelname","Home")/'+id;
});
$(".GoDelete").click(function () {
var id = $(this).attr("data-id");
$('#hfId').val(id);
$('#myModal').modal('show');
});
});
</script>
}
Snippet of the Controller method:
public IActionResult deletelname(pinfo pinfo)
{
var fsname = db.pinfo;
var lsname = db.pinfo;
var csomment = db.pinfo;
foreach (var fname in fsname)
{
db.Remove(fname);
}
foreach (var lname in lsname){
db.Remove(lname);
}
return View("Contact", "Home");
}
Whenever I attempt to use the delete button, I encounter the following error message: