Can't even get basic example working in ASP.NET MVC5
Can't even get basic example working in ASP.NET MVC5
Hi, I am using DT 1.9 Editor in ASP.NET MVC5 but I am struggling to get a simple example Datatable working but I can't see why.
I have a DB table with the following fields
id, alarm_id, device_id, start_date, end_date
I then have the following model class with properties that match the table fields
public class Alarm
{
public int id { get; set; }
public int alarm_id { get; set; }
public int device_id { get; set; }
public string start_date { get; set; }
public string end_date { get; set; }
}
This is my controller method to pass data to the view
[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
[ValidateAntiForgeryToken()]
public ActionResult Data()
{
var formData = HttpContext.Request.Form;
using (var db = new Database("mysql", _dbFactory.CreateConnection(ConnectionDatabase.CS)))
{
var editor = new Editor(db, "alarms", "id")
.Model<Alarm>("Alarm")
.Field(new Field("Alarm.alarm_id"))
.Field(new Field("Alarm.device_id"))
.Field(new Field("Alarm.start_date")
.GetFormatter(Format.DateSqlToFormat(Format.DATE_ISO_8601))
.SetFormatter(Format.DateFormatToSql(Format.DATE_ISO_8601)));
var response = editor.Process(formData).Data();
return Json(response, JsonRequestBehavior.AllowGet);
}
}
Then I have the following function in jQuery in my View
var editor;
$(document).ready(function () {
editor = new $.fn.dataTable.Editor({
"ajax": {
"url": "@Url.Action("Data", "AIR", new { boid = 1 })",
"type": "POST",
"data": function (d) {
d.__RequestVerificationToken = $('input[name=__RequestVerificationToken]').val();
}
},
table: "#airtable",
fields: [{ label: "Alarm ID",
name: "alarm_id" }]
});
alert('Doing Table');
$('#airtable').DataTable({
"ajax": {
"url": "@Url.Action("Data", "AIR", new { boid = 1 })",
"type": "POST",
"data": function (d) {
d.__RequestVerificationToken = $('input[name=__RequestVerificationToken]').val();
}
},
dom: "Bfrtip",
serverSide: true,
columns: [{ data: "alarm_id" }],
select: true,
buttons: [ { extend: "edit", editor: editor },
{ extend: "remove", editor: editor } ]
});
});
On the page I can see the buttons but I get the following error
DataTables warning: table id=airtable - Unknown field: alarm_id (index 0)
Can someone please point me in the right direction ? Many thanks.
Answers
OK I have it, I needed to remove the "Alarm." in the Field parameters in the Controller.