如何从SQL Server正确恢复日期?

时间:2021-01-14 00:02:41

I created a simple CRUD using ASP.NET MVC and I can save my registers normally. If I look in the Data Base my date was stored like that:

我使用ASP.NET MVC创建了一个简单的CRUD,我可以正常保存我的寄存器。如果我查看数据库,我的日期就像那样存储:

2017-06-01 00:01:23.750

But when I try to Edit the same register whith an async method created by MVC Scaffolding the date appears to bel null:

但是当我尝试使用MVC Scaffolding创建的异步方法编辑相同的寄存器时,日期显示为bel null:

01/01/0001 00:00:00

Then (of course) I get an error:

然后(当然)我收到一个错误:

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

将datetime2数据类型转换为日期时间数据类型会导致超出范围的值。

Thats the async method:

那是异步方法:

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Edit([Bind(Include = "ArtigoId,Titulo,Texto,DataPublicacao,UltimaModificacao,UsuarioModificacao,DataCriacao,UsuarioCriacao")] Artigo artigo)
{
    if (ModelState.IsValid)
    {
        db.Entry(artigo).State = EntityState.Modified;
        await db.SaveChangesAsync();
        return RedirectToAction("Index");
    }
    return View(artigo);
}

Why does it happen? How to correctly recover the date from data base?

为什么会这样?如何从数据库中正确恢复日期?

EDIT:

I changed in Data Base for datetime2(0-7) but that made it accept the value 01/01/0001 00:00:00 and what I want is to know why this value appears instead of 2017-06-01 00:01:23.750.

我在datetime2(0-7)的数据库中进行了更改,但是它接受了值01/01/0001 00:00:00,我想知道为什么会出现这个值而不是2017-06-01 00:01 :23.750。

All the others values in the data base is recovered normally, but not this date.

数据库中的所有其他值都会正常恢复,但不是此日期。

1 个解决方案

#1


0  

The problem occurred because I removed my field in the View and there is nothing to do with the date format in SQL Server.

出现此问题是因为我在视图中删除了我的字段,并且与SQL Server中的日期格式无关。

I put the field back in the view as readyonly:

我将该字段放回到视图中:

<div class="form-group">
    @Html.LabelFor(model => model.DataCriacao, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.EditorFor(model => model.DataCriacao, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
        @Html.ValidationMessageFor(model => model.DataCriacao, "", new { @class = "text-danger" })
    </div>
</div>

The search of the field in the DB is done in the GET method:

在DB中搜索字段是在GET方法中完成的:

// GET: Artigos/Edit/5
public async Task<ActionResult> Edit(Guid? id)
{
    if (id == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }
    Artigo artigo = await db.Artigoes.FindAsync(id);
    if (artigo == null)
    {
        return HttpNotFound();
    }
    return View(artigo);
}

The method I posted was POST after the submit of my button to save the changes and no searches were done on the database.

在我提交按钮后,我发布的方法是POST,以保存更改,并且没有对数据库进行任何搜索。

#1


0  

The problem occurred because I removed my field in the View and there is nothing to do with the date format in SQL Server.

出现此问题是因为我在视图中删除了我的字段,并且与SQL Server中的日期格式无关。

I put the field back in the view as readyonly:

我将该字段放回到视图中:

<div class="form-group">
    @Html.LabelFor(model => model.DataCriacao, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.EditorFor(model => model.DataCriacao, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
        @Html.ValidationMessageFor(model => model.DataCriacao, "", new { @class = "text-danger" })
    </div>
</div>

The search of the field in the DB is done in the GET method:

在DB中搜索字段是在GET方法中完成的:

// GET: Artigos/Edit/5
public async Task<ActionResult> Edit(Guid? id)
{
    if (id == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }
    Artigo artigo = await db.Artigoes.FindAsync(id);
    if (artigo == null)
    {
        return HttpNotFound();
    }
    return View(artigo);
}

The method I posted was POST after the submit of my button to save the changes and no searches were done on the database.

在我提交按钮后,我发布的方法是POST,以保存更改,并且没有对数据库进行任何搜索。