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.
Unfortunately, activation email could not send to your email. Please try again.

Identity Column not updating on the grid.

Thread ID:

Created:

Updated:

Platform:

Replies:

129933 Apr 10,2017 10:55 PM Apr 17,2017 01:07 AM ASP.NET Core 9
loading
Tags: Grid
Leon
Asked On April 10, 2017 10:56 PM

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,

Seeni Sakthi Kumar Seeni Raj [Syncfusion]
Replied On April 11, 2017 07:40 AM

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. 
 


Leon
Replied On April 12, 2017 12:03 AM

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);
        }

David Lorentz
Replied On April 12, 2017 10:33 PM

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);
        }

Seeni Sakthi Kumar Seeni Raj [Syncfusion]
Replied On April 13, 2017 06:28 AM

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. 


Leon
Replied On April 13, 2017 06:42 AM

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);
        }

David Lorentz
Replied On April 13, 2017 10:00 AM

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!

Mani Sankar Durai [Syncfusion]
Replied On April 14, 2017 07:32 AM

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. 


Leon
Replied On April 14, 2017 07:35 AM

David,

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

Seeni Sakthi Kumar Seeni Raj [Syncfusion]
Replied On April 17, 2017 01:07 AM

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. 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.

;