A time interval that does not accept a day in MVC3

I have an MVC3 application in which I want to set a time interval, for example, 2 days and 5 hours. when I enter 02: 05: 00: 00, it gives me the following exception:

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. 

When I enter at 05:00:00, it correctly stores 5 hours in the database. according to MSDN, timespan has a property for several days. How to set the days?

Model:

 public class ProductionTimeVM { [Required] public TimeSpan DefaultTime { get; set; } } 

In my opinion, I just use:

 @Html.TextBoxFor(x => x.DefaultTime) 

For my controller:

 public ActionResult SaveProductionTime(ProductionTimeVM vm) { ProductionTime productionTime = new ProductionTime(); productionTime.Default = vm.DefaultTime; //some more code } 

Any idea?

+4
source share
1 answer

You are probably saving the TimeSpan value for the column with Time in the database. Time can only express the time of day, so you cannot save TimeSpans for more than one day.

I use columns with the bigint data type to store and retrieve TimeSpan ticks. I would also like to know if there is a better alternative. This behavior is like the default for some popular ORMs, and it is misleading to many people that I think.

+7
source

Source: https://habr.com/ru/post/1439335/


All Articles