Date saves as 1900-01-01

Date saves as 1900-01-01

samiamsamiam Posts: 11Questions: 2Answers: 0

Been playing around with Editor and created an example based on the 'Basic Initialization' example. In my situation the receive date often has null values in the table. When you edit a record that has a null date value then click update without entering a date the record is saved with a 1900-01-01 date value. You can see in the response body the ReceiveDate is set to 1900-01-01. Is there a way to POST a NULL instead?

Here is the controller I'm using:

Public Class InvController
    Inherits ApiController
    <Route("api/Inv")> _
    <HttpGet> _
    <HttpPost> _
    Public Function Inv() As IHttpActionResult
        Dim request = HttpContext.Current.Request
        Dim DbType = "sqlserver"
        Dim ConnStrKey = ConfigurationManager.ConnectionStrings("MyDatabase").ToString

        Using db = New Database(DbType, ConnStrKey)
            Dim response = New Editor(db, "Barrel_Inv") _
                            .Model(Of Inventory)() _
                            .Field(New Field("Vendor").Validator(Validation.None())) _
                            .Field(New Field("BatchID").Validator(Validation.None())) _
                            .Field(New Field("LotID").Validator(Validation.None())) _
                            .Field(New Field("ReceiveDate").Validator(Validation.DateFormat(Format.DATE_ISO_8601, _
                                New ValidationOpts() With {.Message = "Please enter a date in the format yyyy-mm-dd" _
                            })) _
                .GetFormatter(Format.DateSqlToFormat(Format.DATE_ISO_8601)) _
                .SetFormatter(Format.DateFormatToSql(Format.DATE_ISO_8601))).Process(request).Data()

            Return Json(response)
        End Using
    End Function
End Class

Here is datatable code:
var editor;

    $(document).ready(function () {
        editor = new $.fn.dataTable.Editor({
            ajax: "/api/Inv",
            table: "#tblResults",
            fields: [{
                label: "Vendor:",
                name: "Vendor"
            }, {
                label: "Batch:",
                name: "BatchID"
            }, {
                label: "Lot:",
                name: "LotID"
            }, {
                label: "Receive Date:",
                name: "ReceiveDate",
                type: "date",
                def: function () { return new Date(); },
                dateFormat: $.datepicker.ISO_8601
            }
            ]
        });

        $('#tblResults').DataTable({
            dom: 'li<"float-right" B>tpr',
            ajax: "/api/Inv",
            columns: [
                { data: "Vendor" },
                { data: "BatchID" },
                { data: "LotID" },
                { data: "ReceiveDate" }
            ],
            select :true,
            processing: true,
            lengthMenu: [[20, 50, 100, 500, -1], [20, 50, 100, 500, "All"]],
            pageLength: 20,
            order: [[0, "asc"]],
            colReorder: {
                realtime: false
            },
            buttons: [
                { extend: 'create', editor: editor },
                { extend: 'edit', editor: editor },
                'selectAll',
                'selectNone',
                {
                    extend: 'colvis',
                    collectionLayout: 'fixed two-column',
                    postfixButtons: ['colvisRestore']
                }
            ]
        });
    });

Here is the Body of the POST:

{"draw":null,"data":[{"DT_RowId":"row_30","Vendor":"","BatchID":"","LotID":"3","ReceiveDate":"1900-01-01"}],"recordsTotal":null,"recordsFiltered":null,"error":null,"fieldErrors":[],"id":null,"meta":{},"options":{},"files":{},"upload":{"id":null}}

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,201Questions: 1Answers: 10,414 Site admin
    Answer ✓

    Hi,

    You can use the NullEmpty set formatter if you want to write null to the database and an empty string is being sent to the server from the client-side.

    Allan

  • samiamsamiam Posts: 11Questions: 2Answers: 0

    Thanks Allen, I saw that function but was struggling to make it chainable with the date format. I think I figured it out now.

This discussion has been closed.