DateTime到DateTime2的转换导致超出范围

时间:2019-04-05 17:42:39

标签: asp.net-mvc

当我更新Datetime值时,我希望从视图返回的空值被视为假日,但问题是它们已提交到数据库,并且发生此异常,因此如何在更新数据库时将其删除?

我的代码:

var wktimes = _context.WorkTimes.Where(wk => wk.OrganizationId == organizationInDb.Id).ToList();


for (int i = 1; i <= wktimes.Count; i++)
     {

         wktimes[i - 1].Day = Request.Form["Day_" + i.ToString()];
         wktimes[i - 1].From = Convert.ToDateTime(Request.Form["From_" + 
       i.ToString()]);

         wktimes[i - 1].To = Convert.ToDateTime(Request.Form["To_" + 
          i.ToString()]);

     //checking if the Day is holiday or not

   if (wktimes[i - 1].From.Year == 0001 && wktimes[i - 1].To.Year == 0001)
                {
                    holidays.Add(wktimes[i - 1].Day);
                    wktimes.Remove(wktimes[i-1]);
                    _context.SaveChanges();
                    continue;
                }

                wktimes[i - 1].OrganizationId = organizationInDb.Id;

                _context.SaveChanges();

            }

0 个答案:

没有答案