The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value

后端 未结 23 1382
旧时难觅i
旧时难觅i 2020-11-28 20:19

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         


        
23条回答
  •  日久生厌
    2020-11-28 20:44

    This problem usually occurs when you are trying to update an entity. For example you have an entity that contains a field called DateCreated which is [Required] and when you insert record, no error is returned but when you want to Update that particular entity, you the get the

    datetime2 conversion out of range error.

    Now here is the solution:

    On your edit view, i.e. edit.cshtml for MVC users all you need to do is add a hidden form field for your DateCreated just below the hidden field for the primary key of the edit data.

    Example:

    @Html.HiddenFor(model => model.DateCreated)
    

    Adding this to your edit view, you'll never have that error I assure you.

提交回复
热议问题