how to use initCreate event to add data to new record?

how to use initCreate event to add data to new record?

bjlapbjlap Posts: 29Questions: 11Answers: 1

Hi,

I am using two datatables with editor to create a master detail page.
Master is a table with records (projects).
Detail is a table with records (activities for the project selected in the master table). If a project is selected in the master table, the detail table is updated with its activities.
Everything works except adding new activities.
The problem I have is that when a new activity is created, the id (projectid) of the selected project in the master table must be added as a foreign key value to the newly created activity.

What I am trying to do is to save the projectid in a global javascript variable when it is selected in the master table. On the initCreate event of the details table I want to use this value to add it to the new activity:

    editorActivities.on('initCreate', function (e, json, data) {
        data.ProjectId = projectId;
    }

The error I get in Chrome is: Uncaught TypeError: Cannot set property 'ProjectId' of undefined.
Do you have an example of how to add data in the 'InitCreate' event?

Thanks,
Bert-Jan

Answers

  • bjlapbjlap Posts: 29Questions: 11Answers: 1
    edited August 2015

    I solved the issue as follows: No longer using the event but sending extra data with ajax request:
    var editorActivities = new $.fn.dataTable.Editor({
    ajax: {
    url: '/Admin/ActivitiesTable',
    data: function (d) {
    d.ProjectId = projectId;
    }

    projectid is sent as data with the ajax request.

    Server side:

     public ActionResult ActivitiesTable(string projectId)
    

    ...
    editor.PreCreate += (sender, e) =>
    {
    e.Values.Add("ProjectId", projectId);
    e.Values.Add("Id", Guid.NewGuid());
    };
    ...

This discussion has been closed.