【问题标题】:Kendo UI Scheduler: An overflow occurred while converting to datetimeKendo UI 调度程序:转换为日期时间时发生溢出
【发布时间】:2014-05-16 13:28:24
【问题描述】:

我正在使用 .NET MVC 和(开源)Kendo UI Scheduler 进行开发。我正在尝试使用 javascript 将调度程序中的事件保存/读取/更新/删除到我的数据库中。

但我遇到了一些麻烦:将事件从调度程序保存到我的数据库时,我收到以下错误:

加载资源失败:服务器响应状态为 500(内部服务器错误):System.Data.SqlServerCe.SqlCeException:转换为日期时间时发生溢出。

当从数据库读取到调度器时:

未捕获的 TypeError:无法调用 null 的方法 'getTimezoneOffset'

我用谷歌搜索了这个,但没有找到解决方案,我关注了http://docs.telerik.com/kendo-ui/getting-started/using-kendo-with/aspnet-mvc/helpers/scheduler/ajax-editing 上的文档,除此之外还阅读了有关 Telerik Kendo UI Scheduler 的所有文档...

我正在使用以下代码:

型号

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations;
using Kendo.Mvc.UI;

namespace Eindwerk.Models
{
    public class BookingViewModel : ISchedulerEvent
    {
        [Key]
        public int TaskID { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }

        private DateTime start;
        public DateTime Start
        {
            get
            {
                return start;
            }
            set
            {
                start = value.ToUniversalTime();
            }
        }

        private DateTime end;
        public DateTime End
        {
            get
            {
                return end;
            }
            set
            {
                end = value.ToUniversalTime();
            }
        }
        public string RecurrenceRule { get; set; }
        public int? RecurrenceID { get; set; }
        public string RecurrenceException { get; set; }        
        public bool IsAllDay { get; set; }
        public int? OwnerID { get; set; }
        public string eventRoom { get; set; }
        public string eventAttend { get; set; }
        public string eventExtra { get; set; }
        public string eventRequest { get; set; }

        public class CalendarDBContext : DbContext
        {
            public DbSet<BookingViewModel> Bookings { get; set; }
        }

    }
}

控制器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Eindwerk.Models;
using Kendo.Mvc.Extensions;
using Kendo.Mvc;
using Kendo.Mvc.UI;
using System.Data.Entity;

namespace Eindwerk.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        private Reports.ReportsDBContext rdb = new Reports.ReportsDBContext();

        // GET: /Reports/
        public ActionResult Index()
        {
            return View(rdb.Events.OrderByDescending(p => p.Id).ToList());
            return View();

        }

        private BookingViewModel.CalendarDBContext db = new BookingViewModel.CalendarDBContext();

        public ActionResult Bookings_Read([DataSourceRequest]DataSourceRequest request)
        {

            using (var sampleDB = db)
            {
                IQueryable<BookingViewModel> Bookings = sampleDB.Bookings.ToList().Select(task => new BookingViewModel()
                {
                    TaskID = task.TaskID,
                    Title = task.Title,
                    Start = DateTime.SpecifyKind(task.Start, DateTimeKind.Utc),
                    End = DateTime.SpecifyKind(task.End, DateTimeKind.Utc),
                    Description = task.Description,
                    IsAllDay = task.IsAllDay

                }).AsQueryable();
                return Json(Bookings.ToDataSourceResult(request), JsonRequestBehavior.AllowGet);


            }
        }

        public ActionResult Bookings_Create([DataSourceRequest]DataSourceRequest request, BookingViewModel task)
        {


            if (ModelState.IsValid)
            {
                using (var sampleDB = db)
                {
                    //Create a new Task entity and set its properties from the posted BookingViewModel
                    var entity = new BookingViewModel
                    {
                        TaskID = task.TaskID,
                        Title = task.Title,
                        Start = DateTime.SpecifyKind(task.Start, DateTimeKind.Utc),
                        End = DateTime.SpecifyKind(task.End, DateTimeKind.Utc),
                        Description = task.Description,
                        IsAllDay = task.IsAllDay


                    };


                    // Add the entity
                    sampleDB.Bookings.Add(entity);
                    //sampleDB.Bookings.AddObject(entity);
                    // Insert the entity in the database
                    sampleDB.SaveChanges();

                    // Get the TaskID generated by the database
                    task.TaskID = entity.TaskID;
                }
            }
            // Return the inserted task. The scheduler needs the generated TaskID. Also return any validation errors.
            return Json(new[] { task }.ToDataSourceResult(request, ModelState), JsonRequestBehavior.AllowGet);


        }

        public ActionResult Bookings_Update([DataSourceRequest]DataSourceRequest request, BookingViewModel task)
        {
            if (ModelState.IsValid)
            {
                using (var sampleDB = db)
                {
                    // Create a new Task entity and set its properties from the posted TaskViewModel
                    var entity = new BookingViewModel
                    {
                        TaskID = task.TaskID,
                        Title = task.Title,
                        Start = task.Start,
                        End = task.End,
                        Description = task.Description,
                        RecurrenceRule = task.RecurrenceRule,
                        RecurrenceException = task.RecurrenceException,
                        RecurrenceID = task.RecurrenceID,
                        IsAllDay = task.IsAllDay,
                        OwnerID = task.OwnerID
                    };
                    // Attach the entity
                    sampleDB.Bookings.Attach(entity);
                    // Change its state to Modified so Entity Framework can update the existing task instead of creating a new one
                    //sampleDB.Entry(entity).State = EntityState.Modified;
                    // Or use ObjectStateManager if using a previous version of Entity Framework
                    sampleDB.Entry(entity).State = EntityState.Modified;
                    // Update the entity in the database
                    sampleDB.SaveChanges();
                }
            }
            // Return the updated task. Also return any validation errors.
            return Json(new[] { task }.ToDataSourceResult(request, ModelState));
        }

        public ActionResult Tasks_Destroy([DataSourceRequest]DataSourceRequest request, BookingViewModel task)
        {
            if (ModelState.IsValid)
            {
                using (var sampleDB = db)
                {
                    // Create a new Task entity and set its properties from the posted TaskViewModel
                    var entity = new BookingViewModel
                    {
                        TaskID = task.TaskID,
                        Title = task.Title,
                        Start = task.Start,
                        End = task.End,
                        Description = task.Description,
                        RecurrenceRule = task.RecurrenceRule,
                        RecurrenceException = task.RecurrenceException,
                        RecurrenceID = task.RecurrenceID,
                        IsAllDay = task.IsAllDay,
                        OwnerID = task.OwnerID
                    };
                    // Attach the entity
                    sampleDB.Bookings.Attach(entity);
                    // Delete the entity
                    //sampleDB.Tasks.Remove(entity);
                    // Or use DeleteObject if using a previous versoin of Entity Framework
                    sampleDB.Bookings.Remove(entity);
                    // Delete the entity in the database
                    sampleDB.SaveChanges();
                }
            }
            // Return the removed task. Also return any validation errors.
            return Json(new[] { task }.ToDataSourceResult(request, ModelState));
        }
    }
}

查看

<!DOCTYPE html>
<html>    
<head>  
    <script>
          $(function () {
            $('#scheduler').kendoScheduler({
                date: new Date(Date.now()),
                startTime: (new Date(2014, 6, 13, 7, 00, 00)),
                height:800,
                views: [{ type: "day", selected: true }, { type: 'week' }, { type: 'month' }],
                timezone: "Etc/UTC",
                dataSource:
                    {
                        transport:
                        {
                            read: { url: "@Url.Action("Bookings_Read","Home")", dataType: "json" },
                            update: { url: "@Url.Action("Bookings_Update","Home")", dataType: "json" },
                            create: { url: "@Url.Action("Bookings_Create","Home")", dataType: "json" },
                            destroy: { url: "@Url.Action("Bookings_Destroy","Home")", dataType: "json" },
                                parameterMap: function (options, operation) {
                                    if (operation !== "read" && options.models) {
                                        return { models: kendo.stringify(options.models) };
                                    }  
                                }
                        },

                    },
                schema: {

                    model: { 
                        id: "TaskID", 
                        fields: { 
                            TaskID: { type: "int" }, 
                            RecurrenceID: {type:"int?"}

                        } 
                    } 
                },
            group: {
                resources: ["Rooms"]
            },
            resources: [
                 {
                    field: "attendees",
                    name: "Attendees",
                    dataSource: [
                        { text: "IMD", value: 1, color: "#f8a398" },
                        { text: "IMS", value: 2, color: "#51a0ed" },
                        { text: "Toerisme", value: 3, color: "#56ca85" }
                    ],
                    multiple: true,
                    title: "Attendees"
                },

                {
                    field: "roomId",
                    name: "Rooms",
                    dataSource: {
                        transport:
                            {
                                read: { url: "@Url.Action("Rooms_Read","Room")", dataType: "json" }
                            }    
                    }
                }
            ]              
          });
        });
     </script>
</head>
<body>

有人知道如何解决这个错误吗?还是一个可行的说明性示例?

非常感谢您的帮助!


数据库:

【问题讨论】:

    标签: javascript asp.net-mvc kendo-ui kendo-scheduler kendo-ui-mvc


    【解决方案1】:

    您的模型在 C# 中看起来不错,但是您的数据库中的 Start 和 End 字段是什么 SQL 类型,因为错误发生在 SQLServerCE 和它出现的 C# 视图模型之间。从this blog 开始,数据库结构(尽管是 SQL Server 2012)如下所示:

    【讨论】:

    • 感谢您的回答!现在我更新了我的数据库以匹配您的示例(类型确实是 datetime 而不是 datetime2)。但是会发生“Uncaught TypeError: Cannot call method 'getTimezoneOffset' of null”的错误。当我添加一条记录时,它会添加到我的数据库中,但我似乎无法在调度程序中显示它,我会将示例添加到我的上面的问题。
    • 小备注:在我的 db Meetings 中,类型设置为 datetime2(7) 但我的 edmx 仍在使用 DateTime?而且我无法修改此原因 datetime2 不在我的 edmx 的类型列表中...这是导致问题的原因吗?好混乱……
    • DateTime2 应该被找到,如果它转换为 DateTime?,这只是意味着数据库列允许 NULL 值。
    • 因此,对于 Kendo 调度程序“事件”,Start 和 [End] 应该始终有一个值,因为它在渲染调度时使用它们来定义事件块 (UI)。你的日期时间?实际上应该是 DateTime(不允许空值)。
    • 我的评论有点混乱,我的意思是:在我的 db Meetings 中,类型设置为 datetime2(7) 但我的 edmx 仍在使用 DateTime。问号只是一个问号,我不是指日期时间?
    【解决方案2】:

    删除线:

    timezone: "Etc/UTC"; 
    

    这条线似乎搞砸了调度器。

    【讨论】:

      猜你喜欢
      • 2011-10-07
      • 1970-01-01
      • 2014-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-14
      相关资源
      最近更新 更多