Telerik MVC Grid中DateTime属性可以为null,如何处理这种null值问题?

2026-03-30 12:581阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计551个文字,预计阅读时间需要3分钟。

Telerik MVC Grid中DateTime属性可以为null,如何处理这种null值问题?

我是Telerik MVC扩展的新手。成功在视图中实现了我的第一个实例。我没有使用Telerik MVC Grid实现第二个视图,但它是绑定到具有两列Nullable类型的类。当我运行我的代码时,视图会出错。

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

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

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

我的代码如下:

查看Telerik MVC Grid的代码

Telerik MVC Grid中DateTime属性可以为null,如何处理这种null值问题?

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

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

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

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?>" %>

这解决了这个问题.

本文共计551个文字,预计阅读时间需要3分钟。

Telerik MVC Grid中DateTime属性可以为null,如何处理这种null值问题?

我是Telerik MVC扩展的新手。成功在视图中实现了我的第一个实例。我没有使用Telerik MVC Grid实现第二个视图,但它是绑定到具有两列Nullable类型的类。当我运行我的代码时,视图会出错。

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

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

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

我的代码如下:

查看Telerik MVC Grid的代码

Telerik MVC Grid中DateTime属性可以为null,如何处理这种null值问题?

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

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

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

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?>" %>

这解决了这个问题.