我有一个 MVC3 应用程序,我想在其中将时间跨度设置为例如 2 天 5 小时。 当我输入 02:05:00:00 时,出现以下异常:
System.OverflowException: SqlDbType.Time overflow. Value '2.05:00:00' is out of range. Must be between 00:00:00.0000000 and 23:59:59.9999999.
当我输入 05:00:00 时,它正确地将 5 小时保存到数据库中。根据 MSDN timespan 有几天的属性(property)。如何正确设置日期?
型号:
public class ProductionTimeVM
{
[Required]
public TimeSpan DefaultTime { get; set; }
}
在我看来我只是使用:
@Html.TextBoxFor(x => x.DefaultTime)
对于我的 Controller :
public ActionResult SaveProductionTime(ProductionTimeVM vm)
{
ProductionTime productionTime = new ProductionTime();
productionTime.Default = vm.DefaultTime;
//some more code
}
有什么想法吗?
最佳答案
您可能将 TimeSpan 值保存到带有 Time 的列中数据库中的数据类型。 Time 只能表示一天中的某个时间,因此不能保存大于一天的 TimeSpans。
我使用 bigint 数据类型的列来存储和检索 TimeSpan 的滴答声。我也想知道是否有更好的选择。这种行为类似于一些流行的 ORM 的默认行为,我认为它误导了很多人。
关于c# - 时间跨度不接受 MVC3 中的一天,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12855704/