How do I preEdit, preCreate, and feild().def() to work with Left join in .Net?
How do I preEdit, preCreate, and feild().def() to work with Left join in .Net?
After adding a table left join feild().def() and preEdit and preCreate function no longer work. Here is my code, maybe I have something misplaced.
public class PersonTableController : ApiController
{
[Route("api/PersonTable")]
[HttpGet]
[HttpPost]
public IHttpActionResult PersonTable()
{
var request = HttpContext.Current.Request;
var settings = Test1.Properties.Settings.Default;
using (var db = new Database(settings.DbType, settings.DbConnection))
{
var editor = new Editor(db, "PersonTable", "PersonID")
.Model<PersonTableModel>("PersonTable");
editor.PreCreate += (sender, e) =>
editor.Field("PersonTable.Name").SetValue(e.Values["PersonTable.FirstName"].ToString() + ' ' + e.Values["PersonTable.LastName"].ToString());
editor.PreEdit += (sender, e) =>
editor.Field("PersonTable.Name").SetValue(e.Values["PersonTable.FirstName"].ToString() + ' ' + e.Values["PersonTable.LastName"].ToString());
DtResponse response = new Editor(db, "PersonTable", "PersonID")
.Model<PersonTableModel>("PersonTable")
.Model<ServiceTableModel>("ServiceTable")
.LeftJoin("ServiceTable", "ServiceTable.ServiceNumber", "=", "PersonTable.ServiceNumber")
.Process(request)
.Data();
return Json(response);
}
}
}
And
$('#ServiceTable tbody').on('click', 'tr', function () {
var data = servTable.row(this).data();
perEditor.field('ServiceTable.ServiceNumber').def(data.ServiceNumber);
perEditor.field('PersonTable.ServiceID').def(data.ServiceID);
perEditor.field('ServiceTable.ServiceName').def(data.ServiceName);
});
//***Person Table functions***
var perEditor = new $.fn.dataTable.Editor({
ajax: '/api/PersonTable',
table: '#PersonTable',
fields: [
{
"label": "ServiceID:",
"name": "PersonTable.ServiceID"
},
{
"label": "UserName:",
"name": "PersonTable.UserName"
},
{
"label": "Name:",
"name": "PersonTable.Name"
},
{
"label": "ServiceName:",
"name": "ServiceTable.ServiceName",
"type": "readonly"
},
{
"label": "ServiceNumber:",
"name": "ServiceTable.ServiceNumber"
},
{
"label": "FirstName:",
"name": "PersonTable.FirstName"
},
{
"label": "LastName:",
"name": "PersonTable.LastName"
},
{
"label": "Status:",
"name": "PersonTable.Status"
},
{
"label": "UserPrivilegesLevel:",
"name": "PersonTable.UserPrivilegesLevel"
},
{
"label": "Title:",
"name": "PersonTable.Title"
}
]
});
var perTable = $('#PersonTable').DataTable({
dom: 'Bfrtip',
ajax: '/api/PersonTable',
columns: [
{
"data": "ServiceTable.ServiceID"
},
{
"data": "PersonTable.UserName"
},
{
"data": "PersonTable.Name"
},
{
"data": "ServiceTable.ServiceName"
},
{
"data": "PersonTable.ServiceNumber"
},
{
"data": "PersonTable.FirstName"
},
{
"data": "PersonTable.LastName"
},
{
"data": "PersonTable.Status"
},
{
"data": "PersonTable.UserPrivilegesLevel"
},
{
"data": "PersonTable.Title"
}
],
select: true,
lengthChange: false,
buttons: [
{ extend: 'create', editor: perEditor },
{ extend: 'edit', editor: perEditor },
{ extend: 'remove', editor: perEditor }
]
});
Thank you for any help.
This discussion has been closed.
Answers
You are creating two
new Editor()
instances. You only one to create one and then keep operating on that same instance.Allan
Now I am getting "System.Collections.Generic.KeyNotFoundException: 'The given key was not present in the dictionary.'"
Does it say which line of code is triggering that error? Can you show me your latest code please?
Allan
Here is what I have changed it to
It is telling me this is causing the error
should be:
its a nested object.
Allan
Still receiving the same error.
And it is telling me
Error CS0021 Cannot apply indexing with [] to an expression of type 'object'
Sorry - I was forgetting that it needs to be cast since
e.Values
is aDictionary<string, object>
- try this:Less attractive that how it would be done in Javascript, but that's the cost of static typing .
Allan