How to pass parent id to child grid new record editor

2 posts, 0 answers
  1. Brad
    Brad avatar
    15 posts
    Member since:
    Mar 2016

    Posted 09 Mar 2016 Link to this post

    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


  2. Brad
    Brad avatar
    15 posts
    Member since:
    Mar 2016

    Posted 10 Mar 2016 in reply to Brad Link to this post

    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.
Back to Top