How can I define a model property as a DateTime and have it work properly?

How can I define a model property as a DateTime and have it work properly?

ahanssens@cps247.comahanssens@cps247.com Posts: 13Questions: 1Answers: 0

I've run into a problem with how the Editor handles dates. It seems to be somewhat related to the discussion in https://www.datatables.net/forums/discussion/27820/editor-how-to-get-dates-working-in-asp-net-mvc, but that other discussion seems to be covering a number of other issues that don't relate to the problem I'm having.

I'm using Editor 1.4.2 with the .NET libraries, and the "simple initialization" -> "basic initialization" demo in the example program.

In the StaffController.Staff() function, the definition of the "start_date" Field object includes "get" and "set" formatters that convert the date to DATE_ISO_8601. This results in a date with the format "YYYY/MM/DD" being returned to the Javascript. When the date has this format, everything works properly.

However, if you comment out the lines that add the "get" and "set" formatters to the Field object (lines 48 and 49 in the codefile), a date with the format "MM/DD/YYYY 12:00:00:AM" is returned to the Javascript. Apparently it can't handle this. The date displays correctly in the table itself (though it now has "12:00:00 AM" after it), but when you try to edit a row, the lightbox that appears has the string "mm/dd/yyyy" for the start date instead of the actual date.

As long as StaffModel.start_date is defined as a string, the formatter can be used to work around this problem. However, if you change StaffModel.start_date to be defined as a DateTime, the formatter no longer helps. The problem is that after the formatter runs, Field._WriteCast() converts the formatted string back to a DateTime object, which then gets converted back to a string during the JSON rendering, which gives it the format "MM/DD/YYYY 12:00:00:AM".

I can use strings to hold date values in my models, but I would much rather have them contain the real data type. Is there a way to work around this problem?

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    Answer ✓

    Hi,

    Thanks for your post!

    What is happening is exactly as you say - the model is used to store the data that will be output to JSON - so if that includes a DateTime field, the JSON data will be based on that. The Field._WriteCast method is effectively converting it back from the formatted string to the date type specified.

    I haven't actually decided on the best way to address this in the library yet - the most obvious approach would be to have the output data based on a separate model - one that uses types that map 1:1 to JSON data (so basically strings and numbers), but that seems rather "clucky" to me.

    At the moment, as you say, the only real approach is to use strings and number types in the model only at this time. That is something I will make clear in the documentation.

    If you have any thoughts on how you would like to see it implement, I would be most interested as I'm drawing a blank at the moment! (Although I'll keep thinking on the topic).

    Regards,
    Allan

  • ahanssens@cps247.comahanssens@cps247.com Posts: 13Questions: 1Answers: 0

    I didn't look at the JavaScript, but can anything be done there so that a string with the format "MM/DD/YYYY 12:00:00:AM" is allowed? I assume it's the time portion of the string that's causing the problem. I was very surprised that the dialog completely fails to handle it, rather than just ignoring the time portion of the string.

    This still wouldn't solve the problem of midnight showing up in the table, however.

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin

    You could use columns.render to modify the data that is displayed in the table. Furthermore, you could also use columns.data to do the same task, while also modifying the underlying data (which is what Editor sees), but this is a little more complicated since you need to handle the set case as well as get.

    The final option is to use ajax.dataSrc and loop over the data, modifying it before DataTables even sees it.

    Generally I've found that it is easier to have the server send and receive the format that you want the client-side to have, since the date formatting options in C# are superb compared to Javascript (without a dependency like MomentJS).

    Allan

This discussion has been closed.