I have added a property to my Customer Model, created a migration and updated the database. The field created in DB is datetime. here is my Model code
public DateTime BirthDate { get; set; }
here is the view that is actually a form where I insert date and other fields and submit the form.
<div class="form-group">
@Html.LabelFor(m => m.Customer.BirthDate)
@Html.TextBoxFor(m => m.Customer.BirthDate, "{0: dd-MM-yyyy}", new { @class = "form-control" })
</div>
and here is the Action in the controller
public ActionResult Save(Customer customer)
{
if (customer.Id == 0)
{
_context.Customers.Add(customer);
}
else
{
var customerInDb = _context.Customers.Single(c => c.Id == customer.Id);
customerInDb = customer;
}
_context.SaveChanges();
return RedirectToAction("Index", "Customers");
}
I have papulated the BirthDate field with 23/04/1976 and debug the application and check the value of customer.BirthDate which is 23/04/1976 12:00:00 AM. The date is note before 1753/1/1 but I m receiving the said exception.
I have also tried the following: Make the field nullable by
public DateTime? BirthDate { get; set; }
the exception is gone but the date is not being saved to the database.
Removed the formate "{0: dd-MM-yyy}" from the view but in vain.
Inserted dates in different formats e.g. 23-Apr-1952, 1985-12-01 and 1987/1/2 but didn't work.
Visual Studio 2013 and Database is LocalDb of visual studio.