Primary Id is not displayed when adding a new record


When a new record is added to the grid it gets displayed at the end of the grid and the ID which is the primary key is blank. Why is the primary key not getting updated?
Example:
The Id is 1149 but it is not displayed in the new row which is causing the row to display at the end of the grid instead fo the beginning. The Id is not assigned until the new record is saved and returned from the server/


After I refresh the page ti displays correctly:





Code:
<div id="UpdateAlert" class="alert alert-success" role="alert" style="display:none">
    <a rel='nofollow' href="#" class="close" data-dismiss="alert">&times;</a>
    <strong>The tool has been updated.</strong>
</div>

<div id="AddAlert" class="alert alert-success" role="alert" style="display:none">
    <a rel='nofollow' href="#" class="close" data-dismiss="alert">&times;</a>
    <strong>The tool has been created.</strong>
</div>


  @{
   //variables


    <ejs-grid id="Grid" dataBound="dataBound" height="510px" allowPaging="true" allowSorting="true" allowFiltering="true" allowGrouping="true" allowReordering="true" allowResizing="false" allowMultiSorting="true" allowPdfExport="true" allowExcelExport="true" showColumnChooser="true" toolbarClick="toolbarClick" printComplete='printComplete' actionComplete="actionComplete" actionBegin="actionBegin" toolbar="@(new List<string>() { "Add", "Edit","Print", "PdfExport", "ExcelExport", "Search","ColumnChooser" })">
       ...
        <e-data-manager json="@Model.Tools.ToArray()" adaptor="RemoteSaveAdaptor" insertUrl="/Home/AddTool" updateUrl="/Home/UpdateTool"></e-data-manager>
        <e-grid-columns>
            <e-grid-column field="LocationDescription" headerText="Location" valueAccessor="locationAccessor" width="120"></e-grid-column>
            <e-grid-column field="LocationID" headerText="Location" visible="false" editType="dropdownedit" edit="@(new {create = "createLocation", read = "readLocation", destroy = "destroyLocation", write = "writeLocation"})"  width="50"></e-grid-column>
            <e-grid-column field="ReferenceID" headerText="Reference" visible="false" editType="dropdownedit" edit="@(new {create = "createReference", read = "readReference", destroy = "destroyReference", write = "writeReference"})" width="50"></e-grid-column>
            <e-grid-column field="ToolID" headerText="ID" isPrimaryKey="true" allowEditing="false" width="50"></e-grid-column>
            ...
        </e-grid-columns>
    </ejs-grid>

}

<script>
    var cancelFlag = true;
    var flag = true;

    function dataBound() {
        if (ej.base.Browser.isDevice && flag) {
            this.autoFitColumns();
            flag = false;
            this.refreshColumns();
        }
        this.autoFitColumns();
    }

    function actionBegin(args) {
        if ((args.requestType === 'beginEdit' || args.requestType === 'add')) {
            for (var i = 0; i < this.columns.length; i++) {
                if (this.columns[i].field === "Feet" || this.columns[i].field === "Inches" || this.columns[i].field === "LocationID" || this.columns[i].field === "ReferenceID" || this.columns[i].field === "InventoryID") {
                    this.columns[i].visible = true;
                }
                if (this.columns[i].field === "GroundLength" || this.columns[i].field === "ToolID" || this.columns[i].field === "LocationDescription") {
                    this.columns[i].visible = false;
                }
            }
        }

    }

    function actionComplete(args) {
        if ((args.requestType === 'beginEdit' || args.requestType === 'add')) {
            for (var i = 0; i < this.columns.length; i++) {
                if (this.columns[i].field === "Feet" || this.columns[i].field === "Inches" || this.columns[i].field === "LocationID" || this.columns[i].field === "ReferenceID" || this.columns[i].field === "InventoryID") {
                    this.columns[i].visible = false;
                }
                if (this.columns[i].field === "GroundLength" || this.columns[i].field === "ToolID" || this.columns[i].field === "LocationDescription") {
                    this.columns[i].visible = true;
                }
            }

            var dialog = args.dialog;
            // change the header of the dialog
            dialog.header = args.requestType === 'beginEdit' ? 'Edit Tool' : 'Add New Tool';

            var grid = document.getElementById('Grid').ej2_instances[0]; 
            // The edit input of the column to be hidden is retrieved using its id(will be set as – ‘Grid element id’ + ‘column field’) 
            var editInput = args.form.querySelector('#' + grid.element.id + 'LocationDescription'); 
            //  The edit input’s parent ‘td’ element is hidden 
            editInput.closest('td').style.display = 'none';
        }


        if (args.action === 'add') {
            document.getElementById("AddAlert").style.display = "block";
            setTimeout(function() { hideAlert(); }, 5000);
        }
        if (args.action === 'edit') {
            document.getElementById("UpdateAlert").style.display = "block";
            setTimeout(function() { hideAlert(); }, 5000);
        }

       
    }

    function hideAlert() {
        document.getElementById("AddAlert").style.display = "none";
        document.getElementById("UpdateAlert").style.display = "none";
    }

  
    }

</script>

controller:
 public IActionResult AddTool([FromBody] CRUDModel<Tool> tool)
        {
            var notes = _repository.GetNotes();
            var newTool = GetToolDetails(tool.Value,notes);
            newTool.Created = DateTime.Now;
            _repository.SaveTool(newTool);
             //tool id is returned after the record is saved.
            GetNotes(newTool, notes, false);

            return Json(newTool);
        }



3 Replies 1 reply marked as answer

AG Ajith Govarthan Syncfusion Team May 7, 2021 02:46 PM UTC

Hi Danyelle, 

Thanks for contacting syncfusion support. 

Query: When a new record is added to the grid it gets displayed at the end of the grid and the ID which is the primary key is blank. Why is the primary key not getting updated? 

Based on your query you are facing primarykey blank issue when you update the primarykey in the sever side. So, before we proceed to your query please share the below details to find the root cause of the issue. 

  1. Have you updated the primaryKey at the server side while adding the record in the Grid component?

  1. In the syncfusion package version 19.1.58 we have added the fix for updating the server side values properly in the Grid component with RemoteSaveAdaptor. So, if you are using the package version below 19.1.58 then please update your package versions to 19.1.58 and let us know if you still face the issue at your end.
 
  1. Share the video demonstration of your requirement.
 
  1. Share the syncfusion package version.
 
Regards, 
Ajith G. 



DA Danyelle May 7, 2021 06:35 PM UTC

The primary key is updated on the server side and the syncfusion version is versions to 19.1.59.

I have by Ids sorted in descending order so when a tool is added it should appear at the top of the grid, however when the tool is added right now it is not displaying a tool id so it is getting added to the bottom of the grid. If you refresh the page then it will appear correctly. I have also tried making the tool Id visible and this makes no difference. 


Attachment: addingToolIssue_6a07868a.zip


SK Sujith Kumar Rajkumar Syncfusion Team May 10, 2021 12:18 PM UTC

Hi Danyelle, 
 
Based on the provided information we could understand that you are adding the primary key in the server side and need to display the added data according to the sort performed on the Grid column. For this case, we suggest you to enable the columns isIdentity property for the primary key column as demonstrated in the below code snippet, 
 
<e-grid-column field="OrderID" isIdentity="true" isPrimaryKey="true"></e-grid-column> 
 
This will consider it as an identity column and disable setting values for the primary key column while adding a new record. The value will be set based on the value assigned in the data base that is returned after performing the add operation. So using this approach the newly added data will be properly displayed in the Grid. 
 
 
We have prepared a sample based on this for your reference. You can find it below, 
 
 
Note: A local MDF file is used in the above sample to bind data to the Grid. So before running the sample, please make sure the connection for this file is established in the server explorer and the correct connection string is provided OrderDbContext.cs model file in the application. 
 
Let us know if you have any concerns. 
 
Regards, 
Sujith R 


Marked as answer
Loader.
Up arrow icon