This is a migrated thread and some comments may be shown as answers.

How to pass parent id to child grid new record editor

1 Answer 392 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Brad
Top achievements
Rank 1
Brad asked on 09 Mar 2016, 07:21 PM

I have a TreeView that displays a list of database records. When a user selects a record, I populate a grid with the related record details. The foreign key is sourceid.

I pass the selected record's id like this:

.Read(read => read.Action("AquisitionNotes_Read", "AquisitionNotes").Data("GetCurrentSourceID"))

The GetCurrentSourceID function is simply:

    function GetCurrentSourceID() {
       return {sourceid: currentSourceID };
    }
That works.

But when I want to add a new record, how do I get the currentSourceID value into the Post? If I try the obvious:

.Create(update => update.Action("AquisitionNotes_Create", "AquisitionNotes").Data("GetCurrentSourceID"))

The value from the editor (0 of course) gets precedence and gets passed.

How do I force my actual sourceid to overwrite the editor's value? Or am I solving the problem in the wrong way?

- Brad

 

1 Answer, 1 is accepted

Sort by
0
Brad
Top achievements
Rank 1
answered on 11 Mar 2016, 12:42 AM
Found the answer with a little help from StackOverflow. I was pointed to this forum post.

Call a JS function from the grid's .Edit event like:
.Events(events => events.Edit("SetDefaultSourceID"))

Then that function returns the value, in my case the foreign key value of the selected TreeView item:
function SetDefaultSourceID(e) {
    if (e.model.isNew()) {
        //set the required field value
        e.model.set("sourceid", currentSourceID);
    }
}

This will return a dynamic default value with each new record. I do not know another way to do it in Razor.
Tags
Grid
Asked by
Brad
Top achievements
Rank 1
Answers by
Brad
Top achievements
Rank 1
Share this question
or