This is a migrated thread and some comments may be shown as answers.

Create getting called before update if create called first

1 Answer 46 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Chris
Top achievements
Rank 1
Chris asked on 06 Nov 2019, 01:00 AM

     I have a grid that is defined as such:

@(Html.Kendo().Grid<JohnstonePortal.Data.Entities.Announcement>()
                    .ToolBar(tools =>
                    {
                        tools.Create();
                    })
                   .Name("grdAnnouncements")
                   .AutoBind(true)
                   .Events(x=> x.Edit("onEdit"))                       
                    .ToolBar(tools =>
                    {
                        tools.Search();
                    })
                   .Columns(columns =>
                   {
                       columns.Bound(c => c.Text).Width(500).Title("Announcements");
                       columns.Command(command =>
                       {
                           command.Edit();
                           command.Destroy();
                       }).Width(250);
                   })
                 .Editable(editable => editable.Mode(GridEditMode.PopUp).TemplateName("_EditAnnouncement"))
                .DataSource(dataSource => dataSource
                .Ajax()
                .Model(model=>
                {
                model.Id(ann => ann.Id);
                })
                .Read(read => read.Action("AnnouncementsRead", "Announcements"))
                .Create(create => create.Action("AddAnnouncement", "Announcements"))
                .Update(update => update.Action("UpdateAnnouncement", "Announcements"))
                .Destroy(destroy => destroy.Action("DeleteAnnouncement", "Announcements"))
                ))

 

and the controller is defined as:

public IActionResult AddAnnouncement([DataSourceRequest]DataSourceRequest request, Announcement announcement)
     {
         if (ModelState.IsValid)
         {
             _db.Add<Announcement>
                 (new Announcement
                 {
                     DateCreated= DateTime.Now,
                     Text = announcement.Text
                 });            
             _db.SaveChanges();
         }
 
         // Return a collection which contains only the newly created item and any validation errors.
         return Json(new[] { announcement }.ToDataSourceResult(request, ModelState));
     }
 
     public IActionResult UpdateAnnouncement([DataSourceRequest]DataSourceRequest request, Announcement announcement)
     {
         if (ModelState.IsValid)
         {
             var editAnnouncement = _db.Announcements.Single(x => x.Id == announcement.Id);
             editAnnouncement.Text = announcement.Text;
             _db.SaveChanges();
         }
 
         // Return a collection which contains only the updated item and any validation errors.
         return Json(new[] { announcement }.ToDataSourceResult(request, ModelState));
     }

 

The odd thing is, if I update a row when the page loads, it behaves as normal but if I create one and then update another one, It calls the add endpoint first with the original model that was passed the first time to it and then calls the update on the new model that was intended. This obviously results in a duplicate of the first item that was created. I am not sure what I am doing incorrectly here.

1 Answer, 1 is accepted

Sort by
0
Tsvetomir
Telerik team
answered on 07 Nov 2019, 01:30 PM

Hi Chris,

I have investigated the provided code snippets and I have noticed that the Id of the firstly created item has not been modified. Therefore, the second time the grid updates the item, it detects that there is another item which has an Id equal to 0 and triggers the Create action for it.

Can you try assigning the Id value returned from the database to the item and see if the issue is still present?

 

Kind regards,
Tsvetomir
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
Grid
Asked by
Chris
Top achievements
Rank 1
Answers by
Tsvetomir
Telerik team
Share this question
or