Telerik MVC Grid - 可以为空的DateTime属性的问题

时间:2010-11-17 18:13:46

标签: asp.net-mvc telerik-mvc

我是Telerik MVC扩展的新手。我已成功在视图中实现了我的第一个实例。我没有使用Telerik MVC Grid实现我的第二个视图,但是绑定到网格的类有2列Nullable类型。当我运行我的代码时,视图会发出错误,如下所示:

传递到字典中的模型项为null,但此字典需要“System.DateTime”类型的非null模型项。

我原本以为这可能是一个渲染问题,模板只适用于DateTime而不是Nullable,但后来我完全取出了显示这些DataTime的任何列?属性。

我的代码如下:

查看Telerik MVC Grid的代码

<%= Html.Telerik().Grid(Model.ScheduledCourseList)
.Name("ScheduledCoursesGrid")
.ToolBar(commands => commands.Insert().ButtonType(GridButtonType.Image).ImageHtmlAttributes(new { style = "margin-left:0" }))
.DataKeys(keys => keys.Add(sc => sc.Id))
.DataBinding(dataBinding =>
    dataBinding.Ajax()
        .Select("_List", "Course")
        .Insert("_InsertAjax", "Course")
        .Update("_UpdateAjax", "Course")
        .Delete("_DeleteAjax", "Course")
)
.Columns(columns =>
{
    columns.Bound(c => c.Id).Width(20);
    //columns.Bound(c => c.ScheduledDateTime).Width(120);
    //columns.Bound(c => c.Location).Width(150);
    columns.Command(commands =>
    {
        commands.Edit().ButtonType(GridButtonType.Image);
        commands.Delete().ButtonType(GridButtonType.Image);
    }).Width(180).Title("Commands");
})
.Editable(editing => editing.Mode(GridEditMode.PopUp))
.Sortable()
.Footer(true) %>

DTO

    public class ScheduledCourseDTO
{
    public int Id { get; set; }
    public int CourseId { get; set; }
    public int CourseProviderId { get; set; }
    public DateTime? ScheduledDateTime { get; set; }
    public DateTime? EndDateTime { get; set; }
    public string Location { get; set; }
    public int SiteId { get; set; }

    public decimal CostBase { get; set; }
    public decimal CostPerAttendee { get; set; }
    public decimal PricePerAttendee { get; set; }
    public int MaxAttendees { get; set; }
    public int CancellationDays { get; set; }

    public bool Deleted { get; set; }
}

有人知道如何解决这个问题吗?

2 个答案:

答案 0 :(得分:6)

我设法在Telerik论坛上找到了解决此问题的方法。如果其他人遇到此问题,请查看以下主题:

http://www.telerik.com/community/forums/aspnet-mvc/grid/problem-with-nullable-datetime-property.aspx#1423387 简而言之,解决方案是您应该在ASP.NET MVC项目的Views / Shared文件夹中有一个EditorTemplates文件夹。 Telerik添加了此EditorTemplates文件夹。在此文件夹中有一个名为DateTime.ascx的模板视图,它继承自System.Web.Mvc.ViewUserControl。问题是模板期望正常的DateTime。要修复它,请将其更改为期望可为空的DateTime,如下所示:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<DateTime?>" %>

这解决了这个问题。

答案 1 :(得分:1)

您还必须删除之前为了DateTime而拥有的任何(不可为空)模板吗?模板被击中。

System.Web.Mvc.ViewUserControl<System.DateTime>