I have a controller:
[HttpPost]
public ActionResult Create(Auction auction)
{
var db = new EbuyDataContext();
db.Auctions.Add(auction);
db.SaveCh
The error is because you haven't actually set those values correctly, make sure you set them depending on your applications locale. (e.g. dd/mm/yyyy for en-GB
, mm/dd/yyyy for en-US
).
It also looks like your database has been set up to use a datetime2
column and not a datetime
column.
You can either:
A) Modify the database to change the type from datetime2
to datetime
B) Change the types in your Model to be datetime2
, by doing:
[Column(TypeName = "DateTime2")]
public DateTime StartTime { get; set; }
[Column(TypeName = "DateTime2")]
public DateTime EndTime { get; set; }
I had the same problem when I tried to save an unassigened DateTime member to the DB, while using EntityFramework. I read the answers here, and I learned that it can be solved by declaring the member as nullable. I tried it, and it worked! So here is a code snap, for those who need it:
public Nullable<DateTime> MyDateTime { get; set; }
or
public DateTime? MyDateTime { get; set; }
Later I could use it like bellow:
if(MyDateTime.HasValue)
{
DoBlaBla(MyDateTime.Value);
}
or just assign a value to it like nothing had happen...
MyDateTime = DateTime.Now;
I ran into this with my system fields (created_on, modified_on) even though my model was defined properly.
The cause of my issue was using the disabled attribute vs readonly. Forms will not submit disabled input values to the controller which resulted in the null value which, in turn, resulted in the min date/time value.
Wrong:
<div class="form-group">
@Html.LabelFor(model => model.CreatedOn, htmlAttributes: new { @class = "control-label col-lg-3" })
<div class="col-lg-9">
@Html.EditorFor(model => model.CreatedOn, new { htmlAttributes = new { @class = "form-control", disabled="disabled" } })
@Html.ValidationMessageFor(model => model.CreatedOn, "", new { @class = "text-danger" })
</div>
</div>
Right:
<div class="form-group">
@Html.LabelFor(model => model.CreatedOn, htmlAttributes: new { @class = "control-label col-lg-3" })
<div class="col-lg-9">
@Html.EditorFor(model => model.CreatedOn, new { htmlAttributes = new { @class = "form-control", @readonly="readonly" } })
@Html.ValidationMessageFor(model => model.CreatedOn, "", new { @class = "text-danger" })
</div>
</div>