0

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

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

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

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

未捕获的类型错误:无法调用 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>

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

非常感谢您的帮助!


数据库:

数据库设计

数据被添加到数据库中,但无法在调度程序中显示

在此处输入图像描述

4

2 回答 2

0

Your model looks OK in C#, but what SQL Types are your Start and End fields in your database because the error is happening between SQLServerCE and the C# view model in which it appears. From this blog, the database structure (albeit SQL Server 2012) is shown like this:

enter image description here

于 2014-05-22T13:37:34.793 回答
0

删除行:

timezone: "Etc/UTC"; 

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

于 2015-04-02T18:35:56.350 回答