I have the following code in my HomeController:
public ActionResult Edit(int id)
{
var ArticleToEdit = (from m in _db.ArticleSet where m.storyId == id se
The issue is that you're using ApplyPropertyChanges
with a model object that has only been populated with data in the form (headline, story, and image). ApplyPropertyChanges
applies changes to all properties of the object, including your uninitialized DateTime
, which is set to 0001-01-01, which is outside of the range of SQL Server's DATETIME
.
Rather than using ApplyPropertyChanges
, I'd suggest retrieving the object being modified, change the specific fields your form edits, then saving the object with those modifications; that way, only changed fields are modified. Alternately, you can place hidden inputs in your page with the other fields populated, but that wouldn't be very friendly with concurrent edits.
Update:
Here's an untested sample of just updating some fields of your object (this is assuming you're using LINQ to SQL):
var story = _db.ArticleSet.First(a => a.storyId == ArticleToEdit.storyId);
story.headline = ArticleToEdit.headline;
story.story = ArticleToEdit.story;
story.image = ArticleToEdit.image;
story.modifiedDate = DateTime.Now;
_db.SubmitChanges();
[Solved] In Entity Framework Code First (my case) just changing DateTime
to DateTime?
solve my problem.
/*from*/ public DateTime SubmitDate { get; set; }
/*to */ public DateTime? SubmitDate { get; set; }
Got this problem when created my classes from Database First approach. Solved in using simply Convert.DateTime(dateCausingProblem) In fact, always try to convert values before passing, It saves you from unexpected values.
In my case, in the initializer from the class I was using in the database's table, I wasn't setting any default value to my DateTime property, therefore resulting in the problem explained in @Andrew Orsich' answer. So I just made the property nullable. Or I could also have given it DateTime.Now in the constructor. Hope it helps someone.
I got this error after I changed my model (code first) as follows:
public DateTime? DateCreated
to
public DateTime DateCreated
Present rows with null-value in DateCreated caused this error. So I had to use SQL UPDATE Statement manually for initializing the field with a standard value.
Another solution could be a specifying of the default value for the filed.
I think the most logical answer in this regard is to set the system clock to the relevant feature.
[HttpPost]
public ActionResult Yeni(tblKategori kategori)
{
kategori.CREATEDDATE = DateTime.Now;
var ctx = new MvcDbStokEntities();
ctx.tblKategori.Add(kategori);
ctx.SaveChanges();
return RedirectToAction("Index");//listele sayfasına yönlendir.
}