I have the following class
public class News
{
[Key]
public int NewsId { get; set; }
[Required(ErrorMessage = "The article title is required.")]
[MaxLength(50)]
public string Title { get; set; }
[Required(ErrorMessage = "The article text is required.")]
[UIHint("MultilineText")]
[MaxLength(500)]
public string Article { get; set; }
[Display(Name = "Days to Expire")]
public int DaysToExpire { get; set; }
public DateTime ArticleDate { get; set; }
}
And the generated CRUD view pages. Here is the code from controller for editing:
[HttpPost]
public ActionResult Edit(News news)
{
if (ModelState.IsValid)
{
db.Entry(news).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(news);
}
When I attempt to edit a record, I receive the following error:
The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value.
I changed the method to
[HttpPost]
public ActionResult Edit(News news)
{
if (ModelState.IsValid)
{
var prevDate = db.NewsArticles.First(a => a.NewsId == news.NewsId).ArticleDate;
news.ArticleDate = prevDate;
db.Entry(news).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(news);
}
to try and set the ArticleDate property to what it already is, but received the following error
The statement has been terminated.
An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.
I found this question which is the same error (interestingly they were working with articles as well) and it mentions the error is due to ApplyPropertyChanges being used.
To add, in Edit.cshtml, the ArticleDate is not present as an editable field.
Help is appreciated.
Thank you.