简体   繁体   中英

Data is being saved in SQL DB as 1/1/0001 12:00:00 AM

I'm new to ASP.NET Core and like any other language working with the date/time is a pain. I've an HTML form in which I've fields that when loads look like this:

在此处输入图像描述

The code behind the last two fields ie Date Joined and Last Changed is:

<input class="form-control-sm w-100" id="date_joined" value="@(Employee.DateJoined == DateTime.MinValue ? DateTime.UtcNow : Employee.DateJoined)" autocomplete="off" />
<input class="form-control-sm w-100" id="last_changed" value="@DateTime.Now" readonly="readonly" />

As you can see the Last Changed field is readonly but user can change the date in Date Joined field. Now when the save button is clicked the value of Date Joined is saved successfully but the value of Last Changed is saved as 1/1/0001 12:00:00 AM . Here is the controller code of what happens when save button is clicked:

public IActionResult SaveEmployee(Employee employee) {
            try
            {
                _db.Employees.Add(employee);
                _db.SaveChanges();
                return Json(new { success = true, message = "Saved Successfully" });
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return Json(new { success = false, message = "Error while saving" });
            }
}

Now the data of employee is coming from type script:

   private save() {
        try {
            const employee = this.createEmployee();

            Util.request(this.urlSaveEmployee, 'post', 'json', (response) => {
                if (response != null) {
                    $.notify(response.message);
                    location.reload();
                } else {
                    $.notify(response.message);
                    console.error('Failed to get data #T7G985. Please try again.');
                }
            }, () => { }, employee);
        } catch (e) {
            console.error(e);
        }
    }

    private createEmployee() {
        try {

            const employee = {
                Firstname: $('#first_name').val(),
                Lastname: $('#last_name').val(),
                Position: $('#position').val(),
                Department: $('#department').val(),
                Salary: $('#salary').val(),
                DateJoined: $('#date_joined').val(),
                LastChanged: $('#last_changed').val()
            };

            return employee;
        } catch (e) {
            console.error(e);
        }
    }
}


Any idea why it is still saving the date in min value of DateTime?
UPDATE

Through a breakpoint I can see that the typescript is returning the correct value. 在此处输入图像描述

Also when employee is passed to controllder from type script this is where it is showing the date wrong.

在此处输入图像描述

Your model Employee has property as LastUpdated and your are passing LastChanged from the javascript .

Update post object property LastChanged to LastUpdated then it will work.

Your createEmployee() will updated like below.

private createEmployee() {
    try {

        const employee = {
            Firstname: $('#first_name').val(),
            Lastname: $('#last_name').val(),
            Position: $('#position').val(),
            Department: $('#department').val(),
            Salary: $('#salary').val(),
            DateJoined: $('#date_joined').val(),
            LastUpdated: $('#last_changed').val()  // Updated property name
        };

        return employee;
    } catch (e) {
        console.error(e);
    }
}

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