简体   繁体   中英

Telerik Kendo UI ASP.NET MVC Grid - Event handling of Saved Data Item

I am using the ASP.NET MVC Kendo library with the Grid control, with GridEditMode.PopUp mode for adding/editing. I have a 20+ screens that use the control, which I allow Create and Update actions on. Most models have date ranges, Start and End dates which are DateTime objects in EntityFramework.

Question in a nutshell:

How can I get an Added/Edited record to be refreshed FROM the server immediately after Saving it? This question is independent from the details of my actual issue described below. I want the Saved record to go to the server, then be "Read" back from the server so that all events that normally fire when loading the grid get applied to this one record.

My Create/Update events do a DB save, then simply: return Json(new[] { myModelObject }.ToDataSourceResult(request, ModelState));

I have tried:

  • onSync - Thought it would trigger a grid refresh so that the new record is pulled from the server.

    • Technically solves the issue. The new record has the date as it was entered. Bad because you have to wait on a grid refresh, and you lose the position of the new/edited record (it gets sorted to wherever it belongs).

    • Even worse: if ModelState errors are added on the server, this somehow bypasses all of that. What happens is the Response comes back, flyover errors are shown, but the Sync is triggered (I assume because Client Validation succeeded, and it doesn't know how to see the ModelState errors). Sync triggers a grid refresh. The grid refresh closes the Add popup window. So, the user sees the window disappear.

  • AutoSync(true) - found this on the Grid object and thought it sounded like the solution. I would think it would Sync when saving a record. However, it triggers my Create Action when simply clicking an "Add" button to load the popup window in the first place. Unexpected behavior and can't think of a use case for that

I would like to know how to accomplish this, even if the problem I'm trying to address below can be circumvented in another way. I think there should be a way to access the Saved dataItem, and I think I will need to do this in the future.

Specifically why I am wanting to do this in the first place:

I want any Date input into an Add/Edit dialog to completely ignore the concept of time. If I am in Eastern Time and enter 12/01/2018, then "12/01/2018 00:00:00" is what I want to POST. If a user opens this record in Pacific Time, they see "12/01/2018 00:00:00". I do not want to convert everything to a string since this feels like bad practice, and then sorting/filtering in Kendo would not work properly (I assume).

Background: My problem is when Adding or Editing, the Date in the grid is offset to the local timezone of the machine. So you may add a new record, enter 12/01/2018 in a DatePicker (no time is available) and then in the Grid, the top row shows 11/30/2018. Using the console and looking at the object, I see [date] Thu Nov 30 2018 23:00:00 GMT-0600 (Central Standard Time) . Yet, the Controller receives 12/01/18 00:00:00 (which is correct).

I had already addressed this issue with Reads, because initially EVERY record in my grid was being offset (users noticed every date was 1 day prior to the actual date in the database). I found some articles that indicated using the requestEnd event to loop through values, and for "/Date(" strings, replace the numeric value by kendo.timezone.apply(new Date(parseInt(n)), "Etc/UTC").getTime(); . This works great….when the Grid refreshes from the server. I can have EntityFramework set the DateTime.Kind to UTC on all DateTime objects, then kendo offsets all local Date objects to UTC date objects. Brings it back to midnight on the correct day.

However, add/edit do NOT refresh from the server. So EF events and the Kendo requestEnd event don't happen when Adding/Editing. It seems that Kendo POSTs the data to the server, then says "I already know these values, so I am going to just copy from the form input fields directly to the grid". Efficient, but it bypasses my offset logic.

Here is an example of a Model property:

[Column(Order = 10, TypeName = "smalldatetime")]
[UIHint("Date")]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
[DisplayName("Start Date")]
public DateTime startDate { get; set; }

Do you return a empty object after the update?:

return Json(new[] { myModelObject }.ToDataSourceResult(request, ModelState));. 

Instead of returning the updated object (that telerik will update the local data with and hopefully automatically fix the problem?).

return Json(service.Read(updatedId).ToDataSourceResult(request, ModelState));

I normally don't return the updated object because I would like to get the same "fixes" as a normal read... And when you return an object I'm guessing that telerik will run the requestEnd and fix the date on the updated record?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM