Kendo UI Scheduler:转换为datetime时发生溢出

时间:2014-05-16 13:28:24

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

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

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

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

同时从数据库读取调度程序时:

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

我用Google搜索但却找不到任何解决方案,我按照文档编写:http://docs.telerik.com/kendo-ui/getting-started/using-kendo-with/aspnet-mvc/helpers/scheduler/ajax-editing并阅读有关Telerik Kendo UI Scheduler的所有相关文档......

我使用以下代码:

MODEL

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; }
        }

    }
}

CONTROLLER

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>

有人知道如何解决此错误?还是一个有说服力的例子?

非常感谢您的帮助!


数据库:

database design

data gets added into db but unable to display in scheduler

enter image description here

2 个答案:

答案 0 :(得分:0)

您的模型在C#中看起来没问题,但是SQL类型是数据库中的“开始”和“结束”字段,因为错误发生在SQLServerCE和C#视图模型之间。从this blog开始,数据库结构(尽管SQL Server 2012)显示如下:

enter image description here

答案 1 :(得分:0)

删除该行:

timezone: "Etc/UTC"; 

这条线似乎搞了调度程序。