My code delete the whole table due to usage of each function. I want to delete multiple rows by clicking each delete image link in cell which is mentioned in my view.
If I do not use each function it only delete the first row on which I have clicked and after that if I want to delete another row I have to refresh the page.
my controller
[HttpPost]
public ActionResult Delete(int? id)
{
using (var db = new DAL.InventoryDBEntities())
{
DAL.DetailsTable personalDetail = db.DetailsTable.Find(id);
db.DetailsTable.Remove(personalDetail);
db.SaveChanges();
return Json(true ,JsonRequestBehavior.AllowGet);
}
}
my script
function Delete() {
$("a.deleteimage").each(function () {
var Id = $(this).data("model-id");
var url = "/Home/Delete/" + Id;
$.ajax({
url: url,
type: "POST",
success: function () {
$("#tblDetails").load(window.location + " #tblDetails");
},
error: function () {
alert("Fails");
}
});
});
};
my razor view
<table id="tblDetails" class=" table-bordered table-striped table-condensed">
<thead>
<tr>
<th>Name</th>
<th>City</th>
<th>Country</th>
<th>Options</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>@item.Name</td>
<td>@item.City</td>
<td>@item.Country</td>
<td>
<a id="editimage" href="javascript:;">
<img id="image" src="/UserImages/edit.png" />
</a><a class="deleteimage" data-model-id="@item.Id">
<img src="/UserImages/delete.png" />
</a>
</td>
</tr>
}
</tbody>
</table>