具有编辑操作的Customercontroller(尝试保存日期)...如何从datetime2转换为datetime [英] Customercontroller with edit action (trying to save a date) ... How to convert from datetime2 to datetime

查看:107
本文介绍了具有编辑操作的Customercontroller(尝试保存日期)...如何从datetime2转换为datetime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用带有编辑视图操作的控件,如下所示:

I'm currently using a control with an Edit view action that looks like this:

public async Task<ActionResult> Edit([Bind(Include = "Id,Name,IsSubscribedToNewsLetter,Birthdate,MembershipTypeId")] Customer customer)
{
    if (ModelState.IsValid)
    {
        db.Entry(customer).State = EntityState.Modified;
        await db.SaveChangesAsync();
        return RedirectToAction("Index");
    }
    ViewBag.MembershipTypeId = new SelectList(db.MembershipTypes, "Id", "Id", customer.MembershipTypeId);
    return View(customer);
}



这是错误:

将datetime2数据类型转换为datetime数据类型超出范围。



声明已被终止。



它显示为如果在编辑视图中使用DateTime2但在尝试保存时,由于表中的字段被定义为简单的DateTime,它无法进行转换。



是否有某种方法使用CodeFirst迁移来指定字段实际上是DateTime2而不是DateTime(在模型定义中...使用数据注释的东西可能?



谢谢,



CT



我尝试过:



我在上面指定了...


Here is the error:
The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value.

The statement has been terminated.

It appears as if DateTime2 is being used in the Edit view but when attempting to save, since the field in the table is defined as a simple DateTime, that it cannot make the conversion.

Is there some way using CodeFirst migrations to specify that a field is actually DateTime2 instead of DateTime (in the Model definition ... something using Data Annotations maybe?

Thanks,

C. T.

What I have tried:

What I have tried is specified above ...

推荐答案

c# - 如何使用DbContext和SetInitializer修复datetime2超出范围的转换错误? - 堆栈溢出 [ ^ ]


这篇关于具有编辑操作的Customercontroller(尝试保存日期)...如何从datetime2转换为datetime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆