0

I have 3 DateTime properties in my ViewModel, which is used to generate a report using jqxgrid. The report is correctly generated with all the columns showing. The report can be exported to excel using jqxgrid's 'exportdata' method. The Json that is generated contains the correct information, but after deserializing it, the 'Destruction Date' DateTime property has all values set to null, and the other DateTime properties are rendered properly.

Here is my ViewModel:

    public class GetHMCTSCasesByDestructionDateVM
    {
        [JsonProperty("Fraud Case ID")]
        public int FraudCaseID { get; set; }

        public string Location { get; set; }
        
        public string Region { get; set; }

        [JsonProperty(PropertyName = "Case Closed Date")]
        public DateTime? CaseClosedDate { get; set; }

        [JsonProperty(PropertyName = "Police Case Closed Date")]
        public DateTime? PoliceCaseClosedDate { get; set; }

        [JsonProperty(PropertyName = "Destruction Date")]
        public DateTime? DestructionDate { get; set; }
    }

Here is the Javascript on the view.

<script type="text/javascript">
    $(document).ready(function () {
        var url = "@Url.Action("GetHMCTSCasesByDestructionDateJSON")";
        // prepare the data
        var source = {
            datatype: "json",
            datafields: [
                { name: 'Fraud Case ID', type: 'int' },
                { name: 'Location', type: 'string' },
                { name: 'Region', type: 'string' },
                { name: 'Case Closed Date', type: 'date' },
                { name: 'Police Case Closed Date', type: 'date' },
                { name: 'Destruction Date', type: 'date' }
            ],
            url: url,
            data: {
                startDate: '@Model.from.Value.ToShortDateString()',
                endDate: '@Model.to.Value.ToShortDateString()'
            },
            pager: function (pagenum, pagesize, oldpagenum) {
                // callback called when a page or page size is changed.
            }
        };
        $("#jqxgrid").on('rowclick', function (event) {
            if (event.args.rightclick) {
                $("#jqxgrid").jqxGrid('selectrow', event.args.rowindex);
                var scrollTop = $(window).scrollTop();
                var scrollLeft = $(window).scrollLeft();
                contextMenu.jqxMenu('open', parseInt(event.args.originalEvent.clientX) + 5 + scrollLeft, parseInt(event.args.originalEvent.clientY) + 5 + scrollTop);
                return false;
            }
        });
        var dataAdapter = new $.jqx.dataAdapter(source);
        var getLocalization = function () {
            var localizationobj = {};
            localizationobj.currencysymbol = "£";
            localizationobj.decimalseparator = ".";
            localizationobj.thousandsseparator = "";
            return localizationobj;
        }
        $("#jqxgrid").jqxGrid({
            width: '100%',
            source: dataAdapter,
            theme: 'energyblue',
            pageable: true,
            autoheight: true,
            columnsresize: true,
            filterable: true,
            sortable: true,
            showfilterrow: true,
            localization: getLocalization(),
            columns: [
                
                { text: 'Fraud Case Id', datafield: 'Fraud Case ID', filterable: false },
                { text: 'Location', datafield: 'Location', filterable: false },
                { text: 'Region', datafield: 'Region', filterable: false },
                { text: 'Case Closed Date', datafield: 'Case Closed Date', filterable: false, cellsformat: 'dd/MM/yyyy' },
                { text: 'Police Case Closed Date', datafield: 'Police Case Closed Date', filterable: false, cellsformat: 'dd/MM/yyyy' },
                { text: 'Destruction Date ', datafield: 'Destruction Date', filterable: false, cellsformat: 'dd/MM/yyyy'}
            ]
        });
        $("#exportExcel").jqxButton();
        $("#exportExcel").click(function () {
            $("#jqxgrid").jqxGrid('exportdata', 'json', 'DestructionDate', false, null, true, "@Url.Action("ExportToExcel")");
        });
        $('#clearfilteringbutton').click(function () {
            $("#jqxgrid").jqxGrid('clearfilters');
        });
    });
</script>

And here is the code for deserializing after clicking the 'ExportToExcel' info, and sending it to the excel spreadsheet generator:

 var destructionDate = JsonConvert.DeserializeObject<List<GetHMCTSCasesByDestructionDateVM>>(content, new FormatConverter());
                xl.AddDataWorksheet<GetHMCTSCasesByDestructionDateVM>("Sheet1", destructionDate);

Here is an example of the Json going into the deserializer:

{ "Fraud Case Id": "5556", "Location": "Location ... ", "Region": "North West Region", "Case Closed Date": "19/07/2019", "Police Case Closed Date": "19/07/2019", "Destruction Date ": "19/07/2022" },

And after deserialisation, debugging the list of VMs shows:

CaseClosedDate = {19/07/2019 00:00:00}
FraudCaseID = 5556
PoliceCaseClosedDate = {19/07/2019 00:00:00}
Location = "Location ..."
Region = "North West Region"
DestructionDate = null

I've tried all kinds of naming conventions on the model: using JsonProperty names, not using them and setting the jqgrid datafield to "DestructionDate", so that JsonProperty isn't needed. I can't see any differences between the DateTimes that work, and the one that doesn't (apart from the dates are often in the future, although I have tested this also)

Edit: I have changed the field to a string in the VM, and had the content set to "test string" for each record, just to see if I could deserialize a simple string instead, with the same results. So the null value being returned isn't to do with a DateTime formatting issue.

Edit#2 : I tried setting the DestructionDate property in the VM to non-nullable, and this had the effect of deserializing all the DestructionDate values to 01/01/0001 00:00:00. - this is progress, but then the excel spreadsheet wouldn't open, with only a minimal error message from excel (for Mac).

RonCitrus
  • 19
  • 3
  • 2
    Mind you typos: `"Destruction Date"` vs. `'Destruction Date'` vs. `'Destruction Date '` – Ondrej Tucny Jul 19 '22 at 08:52
  • Thanks Ondrej, these were introduced at the time of posting. I had tried other namings, and reverted back to the original for this post. – RonCitrus Jul 19 '22 at 13:18
  • 2
    Actually, the last one was key. I thought it was just the column heading for the screen report, but actually it was stopping the deserialisation of the field. Thanks for the extra pair of eyes! – RonCitrus Jul 21 '22 at 17:52

0 Answers0