I have a problem with Deleting, Editing row from jquery datatable, it keeps telling me this error
the parameters dictionary contains a null entry for parameter 'id' of non-nullable type
I figure out that the problem being kind of passing the id, As when I put the id directly in Ajax code it works well
Here is my Product model:
public partial class Products
{
public int ID { get; set; }
// Other properties removed for brevity
}
And delete method in Controller
public ActionResult delete(int id)
{
db.Configuration.ProxyCreationEnabled = false;
Products d = db.Products.Where(m => m.ID == id).FirstOrDefault<Products>();
db.Products.Remove(d);
db.SaveChanges();
return Json("success", JsonRequestBehavior.AllowGet);
}
Here is my DataTable row where delete button is situated.
{
"data": "id", "render": function (data) {
console.log(data);
return "<a class='btn btn-success' onclick=Editrow(" + data + ") style='margin-left:12px'><i class='glyphicon glyphicon-edit'></i> Edit Record<a/> <a class='btn btn-danger' onclick=DeleteRow(" + data + ")><i class='glyphicon glyphicon-trash'></i> Delete<a/>";
},
Here data is getting undefined.
Here is My Ajax code:
function DeleteRow(id) {
if (confirm("Are You sure to delete this record..?")) {
$.ajax({
type: 'POST',
//url: "/Products/delete/" + id,
url: '@Url.Action("delete", "Products")/' + id,
datatype: 'JSON',
//data: JSON.stringify({ id: id }),
success: function (response) {
if (response == "success") {
alert("Data Deleted successfully..");
window.location.reload();
//$("#myModal").modal('hide');
}
},
error: function (msg) {
alert(msg.responseText);
}
});
}
}
Note:- when I directly add the id I wanna delete in Ajax code like this, it works well
url: '@Url.Action("delete", "Products")/' + 10,
data: { id: id }and leave the@Url.Action()as-is? I didn't recommend using query strings or paths onurl, instead I suggest to usedataproperty setting.DeleteRow(undefined), actually you're passing'@Url.Action("delete", "Products")/' + undefined, which parsed as null parameter and throwing non-nullable type exception afterwards. Where thatDeleteRowfunction called?return "<a class='btn btn-danger' onclick=DeleteRow(" + data + ")><i class='glyphicon glyphicon-trash'></i> Delete<a/>";