DataTables .NET Editor remove action not deleting a record.
DataTables .NET Editor remove action not deleting a record.
I need some help to point out what i am doing wrong or what i am am not doing right,
I have the following code that all works bar the delete action
Controller Method in .NET
[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
[ValidateAntiForgeryToken()]
public ActionResult GetFlagsList()
{
var formData = HttpContext.Request.Form;
using (var db = new Database("mysql", _dbFactory.CreateConnection(ConnectionDatabase.CustomerSpecific)))
{
var editor = new Editor(db, "pmt_flags f", "id")
.Field(new Field("f.id").Set(true))
.Field(new Field("f.source").Set(true)
.Options(new Options()
.Table("table1 invd3")
.Value("id").Label("name").Where(r => {
r.Where("inv_type_id", "(108, 111)", "IN", false);
})
.Order("id")
)
)
.Field(new Field("invd1.name").Set(false))
.Field(new Field("f.destination").Set(true)
.Options(new Options()
.Table("table2 invd3")
.Value("id").Label("name").Where(r => {
r.Where("inv_type_id", "(108, 111)", "IN", false);
})
.Order("id")
)
)
.Field(new Field("invd2.name").Set(false))
.Field(new Field("f.flag")
.Set(true)
.Options(new Options()
.Table("managed_values mv")
.Value("m_value").Label("mv.d_value").Where(r => {
r.Where("mv.mvg_id", "(SELECT id FROM managed_values_groups mvg WHERE mvg.code = 'MIGRATION_FLAGS')", "IN", false);
})
.Order("id")
)
)
.Field(new Field("f.type").Set(true))
.Field(new Field("f.enabled").Set(true))
.Field(new Field("f.description").Set(true))
.Field(new Field("f.create_by").Set(true))
.Field(new Field("f.update_by").Set(true))
.LeftJoin("table1 invd1", "f.source", "=", "invd1.id")
.LeftJoin("table1 invd2", "f.destination", "=", "invd2.id");
editor.PreCreate += (sender, e) =>
{
//if (readOnly)
//{
// e.Cancel = true;
// return;
//}
Dictionary<string, object> flagData = ((Dictionary<string, object>)e.Values["f"]);
User user = GetUser();
editor.Field("f.create_by").SetValue(user.Id);
editor.Field("f.update_by").SetValue(user.Id);
if (!isValidFlag(flagData))
{
// We already have an entry for this service Type
throw new Exception("Flag Could not be created, please check the values and try again.");
}
};
editor.PreEdit += (sender, e) =>
{
Dictionary<string, object> flagData = ((Dictionary<string, object>)e.Values["f"]);
User user = GetUser();
editor.Field("f.update_by").SetValue(user.Id);
if (!isValidFlag(flagData, "edit"))
{
// We already have an entry for this service Type
throw new Exception("Flag Could not be updated, please check the values and try again.");
}
};
editor.PreRemove += (sender, e) =>
{
Dictionary<string, object> flagData = ((Dictionary<string, object>)e.Values["f"]);
User user = GetUser();
if (!isValidFlag(flagData, "edit"))
{
// We already have an entry for this service Type
throw new Exception("Flag Could not be created please check the values and try again.");
}
};
var response = editor.Transaction(true).Debug(true).Process(formData).Data();
return Json(response, JsonRequestBehavior.AllowGet);
}
}
Java Script
var isAdmin = @Html.Raw(Json.Encode(Model.isAdmin));
var editor = new $.fn.dataTable.Editor({
"ajax": {
"url": "@Url.Action("GetFlagsList", "CustomerMigration", new { boid = 1 })",
"type": "POST",
"data": function (d) {
d.__RequestVerificationToken = $('input[name=__RequestVerificationToken]').val();
}
},
table: "#tblSC",
fields: [
{
name: "f.id",
type: "hidden"
},
{
name: "f.source",
type: "select",
placeholder: "--- select a source ---"
},
{
name: "f.destination",
type: "select",
placeholder: "--- select a destination ---"
},
{
name: "f.flag",
type: "select",
placeholder: "--- select a flag ---"
},
{
name: "f.type",
type: "select",
placeholder: "--- select a type ---",
options: [
{ label: "VOICE", value: "VOICE" },
{ label: "PON", value: "PON" },
{ label: "DATA", value: "DATA" },
{ label: "L3", value: "L3" }
]
},
{
name: "f.description",
type: "textarea",
},
{
name: "f.enabled",
type: "select",
placeholder: "--- select a enabled ---",
options: [
{ label: "Yes", value: 1 },
{ label: "No", value: 0 }
]
},
]
});
var table = $('#tblSC').DataTable({
ajax:
{
"url": "@Url.Action("GetFlagsList", "CustomerMigration", new { boid = 1 })",
"type": "POST",
"data": function (d) {
d.__RequestVerificationToken = $('input[name=__RequestVerificationToken]').val();
},
},
serverSide: true,
searching: true,
orderable: true,
processing: true,
dom: '<"row"<"col-sm-6"B>><"row-fluid"<"col-sm-6"l><"col-sm-6"f>>rt<"row-fluid"<"col-sm-6"i><"col-sm-6"p>>',
select: true,
columns: [
{ "data": "f.id", "searchable": true},
{ "data": "invd1.name", "searchable": true},
{ "data": "invd2.name", "searchable": true },
{ "data": "f.flag", "searchable": true, render: function (data) {
switch (data) {
case "0": default: return "<i class='fa fa-circle flag-green'></i>";
case "1": return "<i class='fa fa-circle flag-amber'></i>";
case "2": return "<i class='fa fa-circle flag-red'></i>";
}
}
},
{ "data": "f.type", "searchable": true },
{ "data": "f.enabled", "searchable": true, render: function (data) {
switch (data) {
case "0": default: return "<i class='fa fa-times text-danger'></i>";
case "1": return "<i class='fa fa-check text-success'></i>";
}
}
}
],
buttons: [
{ extend: "create", editor: editor },
{ extend: "edit", editor: editor },
{ extend: "remove", editor: editor },
],
initComplete: function () {
$(".dt-buttons").prepend('<button class="btn btn-secondary show-text-hover" data-href="@Url.Action("Index", "CustomerMigration", new { boid = Model.CompanyId })" type="button"> <i class="fa fa-arrow-left"></i> <span>Back</span> </button>')
if (!isAdmin) {
$('.buttons-create').addClass('disabled'); // Disable the button visually if isAdmin is false
}
}
});
});
the table loads correctly i can edit and create with no issues, but not the remove action it sends the data in the request with the action remove with the row data, and then i get the second reload the table data, but the record to be deleted is still there.
can someone please point me in the right direction
Editor Version: DataTables-Editor-Server.2.0.5 // net48\DataTables-Editor-Server.dll
This question has an accepted answers - jump to answer
Answers
Nothing immediately stands out to me as causing an issue there. Can you show me the data sent to the server and also the JSON response in return please?
Thanks,
Allan
This is the post request sent for the delete
Json Response form that
then there is another request that returns all the table data after this one and that one look normal it returns the rows but the deleted one is not deleted.
Suggests that no query was executed against the database. Which appears to agree with that you are seeing when you reload the page.
I know it is a bit of a pain, but could you try removing the alias for the table name and see if that helps? i.e.
would be:
And wherever you have
f.
in the fields (C# and Javascript) it would need to be updated to bepmt_flags
. I'm not certain that is the issue, but it would be the starting point.Allan
It creates records with the alias in place, but sure will give that a go maybe the delete record is different for some reason.
Yeah, it follows a slightly different code path and I had some issues with aliases in the past. It is possible there is still an issue there. I'll try it later on as well.
Allan
Hey removing the alias worked, can you please make a fix to allow the DELETE to work with aliases as this can only help
Thank you for confirming that and good to hear there is a workaround for the moment.
I've logged this so I can look at it for the next release of Editor.
Allan