Error on CRUD using ForeignKey column

Using the guideline provided in the documentation, while using the Edit Template, show the following error during Add a new record.


https://ej2.syncfusion.com/aspnetmvc/documentation/grid/columns/foreign-key-column#use-edit-template-in-foreign-key-column



Image_2914_1740421678858


Thank you.


Attachment: WebApplication1_2_f9b98f38.zip

4 Replies 1 reply marked as answer

RO Rodrigo February 26, 2025 06:48 AM UTC

Hi, any updates? Thank you.


Regards.



SI Santhosh Iruthayaraj Syncfusion Team February 26, 2025 07:08 AM UTC

Hi Rodrigo,

Greetings from Syncfusion Support.

The script error encountered in the edit template when adding a new record occurs because the foreignKeyData property in the arguments is undefined. This property contains a valid object only when editing a record, as the record already exists in the dataSource with stored values. However, when adding a new record, no prior data is available in the dataSource, as the record is created during the add action. Consequently, foreignKeyData remains undefined, and any attempt to access its properties results in the current issue.

To resolve this, you can include a conditional check to verify whether foreignKeyData is defined before accessing its values. Please refer to the modified code snippet below:


[Views\Home\Index.cshtml]

 

            function write (args) { // to show the value for custom component

                if (args.foreignKeyData) {

                    var value = args.foreignKeyData[0][args.column.foreignKeyValue];

                }

                autoComplete = new ej.dropdowns.AutoComplete({

                    dataSource: employeeData,

                    fields: { value: args.column.foreignKeyValue },

                    value: value

                });

                autoComplete.appendTo(args.element);

            }

 


Please let us know if you require any further assistance.

Regards,
Santhosh I


Marked as answer

RO Rodrigo replied to Santhosh Iruthayaraj February 28, 2025 06:38 AM UTC

Hi Santhosh, the solution provided works great.


Thank you.



SI Santhosh Iruthayaraj Syncfusion Team March 3, 2025 04:47 AM UTC

Hi Rodrigo,


We are glad to hear that the provided solution was helpful. Please let us know if you require any further assistance.


Regards,
Santhosh I


Loader.
Up arrow icon