We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Identity Column not updating on the grid.

The identity column is not updating after a record is inserted in the database. On the data base side, all works as expected. But since the grid is not getting the key, this prevents editing or deleting a record recently inserted.I am working with: Syncfusion.EJ.AspNet.core: 15.1600.0.33Thanks,

9 Replies

SS Seeni Sakthi Kumar Seeni Raj Syncfusion Team April 11, 2017 11:40 AM UTC

Hi Juan,  
 
Thanks for contacting Syncfusion Support.  
 
We could see you are not returning an inserted record to the Grid which is the cause of the problem. Since you are using the is-identity for the PrimaryKey column, the value in the client-side differs from the server-side and to notify the changes on the is-identity column you have to return them from server-end. Refer to the following code example. 
 
 
<ej-grid id="Grid1" datasource="ViewBag.order" allow-paging="true"> 
    <e-datamanager json="ViewBag.order"  
                   insert-url="/Home/CellEditInsert"  
                   remove-url="/Home/CellEditDelete"  
                   update-url="/Home/CellEditUpdate"  
                   adaptor="remoteSaveAdaptor"/> 
                . .   
    <e-columns>  
        <e-column field="ID" header-text="Employee ID" is-identity="true" is-primary-key="true"></e-column> 
           .  ..  
                  . . . 
    </e-columns> 
</ej-grid> 
 
 
        public ActionResult CellEditInsert([FromBody]CRUDModel<Orders> value) 
        { 
            Random ran = new Random(); 
            //for an example, we have used 
            //Random Number 
            value.Value.ID = ran.Next(); 
            order.Insert(0, value.Value); 
            return Json(value.Value); 
        } 
 
 
Regards,  
Seeni Sakthi Kumar S. 
 



LE Leon April 12, 2017 04:03 AM UTC

The code below is what I am using. In debug mode when I check the key value in the object I am returning back, the key value is correct. Somehow it gets lost in the response process.  If I refresh the page: the key gets populated but of course that is not the desired behavior.       

 [HttpPost]
        public async Task<IActionResult> SF_Create([FromBody]CRUDModel<MedHistory> value,
            [Bind("IdRecordMedHistory,ActivityLevel,AdmisionDate,AdmisionTime,Age,Asthma,DonorHospitalName,DonorId,DrugUse,HepatitisCbhiv,OtherPulmonaryDiseases,PreviousCardiothoracicSurgery,PreviousIntubation,SmokingHistory")] MedHistory medHistory)
        {
            try
            {
                
                if (ModelState.IsValid)
                {
                    _context.Add(value.Value);
                    await _context.SaveChangesAsync();
                    return Json(value);
                }
            }
            catch (DbUpdateException /* ex */)
            {
                //Log the error (uncomment ex variable name and write a log.
                ModelState.AddModelError("", "Unable to save changes. " +
                    "Try again, and if the problem persists " +
                    "see your system administrator.");
            }

            return View(medHistory);
        }


DL David Lorentz April 13, 2017 02:33 AM UTC

I seem to be having the same issue as Leon.  Code posted below.

<ej-grid id="FlatGrid" allow-paging="true">
    <e-datamanager json="ViewBag.dataSource" update-url="/Home/Update" insert-url="/Home/Insert" remove-url="/Home/Delete" adaptor="remoteSaveAdaptor"></e-datamanager>
    <e-edit-settings allow-adding="true" allow-editing="true" allow-deleting="true" edit-mode="Dialog"></e-edit-settings>
    <e-toolbar-settings show-toolbar="true" toolbar-items='@new List<string> {"add","edit","update","cancel", "delete"}' />
    <e-columns>
        <e-column field="Id" header-text="Id" is-primary-key="true" is-identity="true"></e-column>
        <e-column field="LastName" header-text="Last Name" width="110"></e-column>
        <e-column field="FirstName" header-text="First Name" width="110"></e-column>
    </e-columns>
</ej-grid>

public IActionResult Insert([FromBody] CRUDModel<Person> model)
        {
            // Assign a value to Id for testing
            model.Value.Id = 63;
            return Json(model.Value);
        }


SS Seeni Sakthi Kumar Seeni Raj Syncfusion Team April 13, 2017 10:28 AM UTC

Hi Juan/David, 
 
We are able to reproduce the problem when the values were returned in the following format.  
 
        public ActionResult CellEditInsert([FromBody]CRUDModel<Orders> value) 
        { 
            Random ran = new Random(); 
            //for an example, we have used 
            //Random Number 
            value.Value.ID = ran.Next(); 
            order.Insert(0, value.Value); 
            return Json(value); 
        } 
 
In this case, value also holds other parameters like Action, and other parameters including Value. This is not recognized by the Grid while updating the records with the primarykey value.  
 
 
 
So we suggest to change your code as follows.  
 
        public ActionResult CellEditInsert([FromBody]CRUDModel<Orders> value) 
        { 
            Random ran = new Random(); 
            //for an example, we have used 
            //Random Number 
            value.Value.ID = ran.Next(); 
            order.Insert(0, value.Value); 
            return Json(value.Value); 
        } 
 
We have also prepared a sample that can be downloaded from the following location. 
 
 
If you are still facing any problem, please share the following details to analyze the problem at our end.  
 
1)      Complete code example of the Grid and code behind 
2)      Screenshot for the response sent back from the server (after inserting records) – similar to above screenshot 
3)      Screenshot of the Grid 
4)      Grid model 
5)      If possible, modify the attached sample and reproduce the issue 
 
Regards,  
Seeni Sakthi Kumar S. 



LE Leon April 13, 2017 10:42 AM UTC

Still not working... Same behavior

        [HttpPost]
        //[ValidateAntiForgeryToken]
        public async Task<IActionResult> SF_Create([FromBody]CRUDModel<MedHistory> value,
            [Bind("IdRecordMedHistory,ActivityLevel,AdmisionDate,AdmisionTime,Age,Asthma,DonorHospitalName,DonorId,DrugUse,HepatitisCbhiv,OtherPulmonaryDiseases,PreviousCardiothoracicSurgery,PreviousIntubation,SmokingHistory")] MedHistory medHistory)
        {
            try
            {
                
                if (ModelState.IsValid)
                {
                    _context.Add(value.Value);
                    await _context.SaveChangesAsync();
                    return Json(value.Value);
                }
            }
            catch (DbUpdateException /* ex */)
            {
                //Log the error (uncomment ex variable name and write a log.
                ModelState.AddModelError("", "Unable to save changes. " +
                    "Try again, and if the problem persists " +
                    "see your system administrator.");
            }

            return View(medHistory);
        }


DL David Lorentz April 13, 2017 02:00 PM UTC

By examining your example, I was able to determine what was causing my issue. 

ASP.NET Core changes property names to camelCase by default when serializing objects to JSON.  This meant that the JSON returned to the browser did not match the JavaScript object feeding the grid.

The solution for me was to add the following to my Startup.cs under ConfigureServices which tells the JSON Serializer to leave the property names alone.

services.AddMvc().AddXmlSerializerFormatters()
           .AddJsonOptions(x =>
           {
               x.SerializerSettings.ContractResolver = null;
           });

Thanks for the help!


MS Mani Sankar Durai Syncfusion Team April 14, 2017 11:32 AM UTC

Hi Daivd/Juan, 

We have prepared a sample by saving the inserted value to database and with editing or deleting the added record using UrlAdaptor that can be downloaded from the below link. 

<ej-grid id="FlatGrid" allow-paging="true"> 
    <e-datamanager url="/Home/DataSource" insert-url="/Home/CellEditInsert" update-url="/Home/CellEditUpdate" remove-url="/Home/CellEditDelete" adaptor="UrlAdaptor" /> 
    <e-edit-settings allow-adding="true" allow-editing="true" allow-deleting="true"></e-edit-settings> 
    <e-toolbar-settings show-toolbar="true" toolbar-items="@(new List<string>() {"add","edit","delete","update","cancel" })"></e-toolbar-settings> 
    <e-columns> 
        <e-column field="orderID" header-text="Order ID" is-identity="true" is-primary-key="true" text-align="Right" width="75"></e-column> 
         <e-column field="employeeID" header-text="Employee ID" text-align="Right"  width="75"></e-column> 
    </e-columns> 
</ej-grid> 
[Controller.cs] 
  public ActionResult CellEditInsert([FromBody]EditParams value) 
        { 
            var datas = _context; 
            value.value.OrderID = _context.Orders.First().OrderID -1; 
            datas.Orders.Add(value.value); 
            datas.SaveChanges(); 
            return Json(value); 
        } 
        public ActionResult CellEditUpdate([FromBody]EditParams value) 
        { 
            var db = _context; 
            db.Entry(value.value).State = EntityState.Modified; 
            db.SaveChanges(); 
            return Json(value); 
 
        } 
        public ActionResult CellEditDelete([FromBody]EditParams value) 
        { 
            var db = _context; 
            Orders order = db.Orders.Where(c => c.OrderID == Convert.ToInt32(value.key)).FirstOrDefault(); 
            db.Orders.Remove(order); 
            db.SaveChanges(); 
            return Json(order); 
        } 

Also in ASP.Net Core CamelCase property of JSONFormatter changes the case of the “Items” to “items” while returning the response from the server-end, so the reported issue may occurs. 
 
If you still face the issue please get back to us with the following details. 
1.       code example of the Grid and code behind  
2.       Screenshot for the response sent back from the server (after inserting records) – similar to above screenshot  
3.       Screenshot of the Grid  
 
The provided information will help us to analyze the issue and provide you the response as early as possible. 
 
Please let us know if you need further assistance. 

Regards, 
Manisankar Durai. 



LE Leon April 14, 2017 11:35 AM UTC

David,

This worked for me too. I appreciate your help and the help of the Syncfusion  team.


SS Seeni Sakthi Kumar Seeni Raj Syncfusion Team April 17, 2017 05:07 AM UTC

Hi David,  
 
Thanks for the update. We are happy to hear that your issue has been resolved. Please get back to us, if you require further assistance on this.  
 
Regards,  
Seeni Sakthi Kumar S. 


Loader.
Live Chat Icon For mobile
Up arrow icon