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?

HgooberHgoober Posts: 11Questions: 3Answers: 0

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.

Answers

  • allanallan Posts: 63,505Questions: 1Answers: 10,471 Site admin

    You are creating two new Editor() instances. You only one to create one and then keep operating on that same instance.

    Allan

  • HgooberHgoober Posts: 11Questions: 3Answers: 0

    Now I am getting "System.Collections.Generic.KeyNotFoundException: 'The given key was not present in the dictionary.'"

  • allanallan Posts: 63,505Questions: 1Answers: 10,471 Site admin

    Does it say which line of code is triggering that error? Can you show me your latest code please?

    Allan

  • HgooberHgoober Posts: 11Questions: 3Answers: 0

    Here is what I have changed it to

    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")
                             .Model<ServiceTableModel>("ServiceTable")
                    .LeftJoin("ServiceTable", "ServiceTable.ServiceNumber", "=", "PersonTable.ServiceNumber");
    
    
                   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());
    
    
    
                    var response = editor                         
                                         
                 
                     .Process(request)
                     .Data();                 
    
                    return Json(response);
                }
            }
        }
    

    It is telling me this is causing the error

  • allanallan Posts: 63,505Questions: 1Answers: 10,471 Site admin

    e.Values["PersonTable.FirstName"]

    should be:

    e.Values["PersonTable"]["FirstName"]
    

    its a nested object.

    Allan

  • HgooberHgoober Posts: 11Questions: 3Answers: 0

    Still receiving the same error.

  • HgooberHgoober Posts: 11Questions: 3Answers: 0

    And it is telling me
    Error CS0021 Cannot apply indexing with [] to an expression of type 'object'

  • allanallan Posts: 63,505Questions: 1Answers: 10,471 Site admin
    edited July 2018

    Sorry - I was forgetting that it needs to be cast since e.Values is a Dictionary<string, object> - try this:

    ((<Dictionary<string, object>)e.Values["PersonTable"])["FirstName"]
    

    Less attractive that how it would be done in Javascript, but that's the cost of static typing :).

    Allan

This discussion has been closed.