1

I have this field in my model :

public DateTime ATime{ get; set; }

On Create Controller, I can pick a date using datepicker and it successfully creates.

But when I try to Edit the object, I get the exception in the title. My Edit code is default Edit code :

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include="AID,ATime")] Project project)
{
            if (ModelState.IsValid)
            {
                db.Entry(project).State = EntityState.Modified;
                db.SaveChanges(); // I get exception at this line.
                return RedirectToAction("Index");
            }
            return View(project);
}

How can I get rid of this exception? Thanks.

jason
  • 6,962
  • 36
  • 117
  • 198
  • 1
    What is the value of `ATime` in the POST method? (I assume its `01/01/0001`) –  Dec 03 '15 at 12:21
  • No it's not `01/01/0001`, it's currently `2016-04-08 00:00:00.0000000` in the database. – jason Dec 03 '15 at 12:24
  • 1
    Not the value in the database. What is the value of `project.ATime` in the POST method. Debug your code. –  Dec 03 '15 at 12:25
  • @StephenMuecke it's this : `16.12.2015 00:00:00` – jason Dec 03 '15 at 12:30
  • Is `ATime` the only `DateTime` property in your model? The error suggests you trying to save a `DateTime` that has a value outside the range `1753 to 9999` but the value you have shown in your last comment is clearly within that range. –  Dec 03 '15 at 12:34
  • @StephenMuecke, you were right, not ATime but another DateTime field was 01/01/0001, but it was another value in the Database, why is it so in the field? – jason Dec 03 '15 at 12:35
  • 1
    Refer [this answer](http://stackoverflow.com/questions/1334143/sql-server-datetime2-vs-datetime) for an explanation of `DATETIME` vs `DATETIME2`. You have 2 options - easy one is to render a hidden input for the other property so it posts back and binds to your model - the correct one is to use a view model with only those properties you edit, then in the POST method, get the original data model from the database, update its properties from the view model, and save the data model. –  Dec 03 '15 at 12:38

3 Answers3

0

Datetime2 has a bigger range than datetime. Are you sure the new value you're trying to set it to fits wtihin datetime's range?

(Also, datetime is a legacy type that's not ANSI compliant. I'd change your underlying type in the database to datetime2 in any case. http://blogs.msdn.com/b/cdnsoldevs/archive/2011/06/22/why-you-should-never-use-datetime-again.aspx)

eftpotrm
  • 2,241
  • 1
  • 20
  • 24
0

Change your modal code as below:-

[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public DateTime ATime{ get; set; }

Also date should be in the range: Minimum Date is : 1 Jan 1753 Maximum Date is : 31 Dec 9999

Akshey Bhat
  • 8,227
  • 1
  • 20
  • 20
0

I assume that you are not using datetime2 type specially. So you can use data annotions to assign datetime type to your model property like below.

[DataType(DataType.DateTime)]
public DateTime ATime { get; set; }
c0demaster
  • 738
  • 6
  • 17
  • So automatic migrationa are not enabled? Run add-migration command then Update-database from package manager console. – c0demaster Dec 03 '15 at 12:34