Timespan不接受MVC3中的一天

mid*_*pat 4 c# timespan asp.net-mvc-3

我有一个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.
Run Code Online (Sandbox Code Playgroud)

当我输入05:00:00时,它正确地将5小时保存到数据库中.根据MSDN timespan有一天属性.我如何正确设置日期?

模型:

public class ProductionTimeVM
{        
    [Required]
    public TimeSpan DefaultTime { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

在我看来,我只是使用:

@Html.TextBoxFor(x => x.DefaultTime)
Run Code Online (Sandbox Code Playgroud)

对于我的控制器:

public ActionResult SaveProductionTime(ProductionTimeVM vm)
{           
   ProductionTime productionTime = new ProductionTime();
   productionTime.Default = vm.DefaultTime;

   //some more code
 }
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

Ufu*_*arı 7

您可能正在将TimeSpan值保存到数据库中具有Time数据类型的列.时间只能表达一天中的某个时间,因此您无法节省超过一天的TimeSpans.

我使用bigint数据类型的列来存储和检索TimeSpan的滴答.我还想知道是否有更好的选择.对于一些流行的ORM来说,这种行为就像默认行为一样,而且我认为很多人误导了这种行为.

  • +1不是'可能'但绝对是因为它是一个`SqlDbType.Time溢出'.最好的办法是将`Ticks`值存储在`bigint`中 (2认同)