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

发布于 2024-10-03 04:24:48 字数 1986 浏览 7 评论 0原文

我对 Telerik MVC 扩展相当陌生。我已经在视图中成功实现了我的第一个实例。我没有使用 Telerik MVC 网格实现第二个视图,但是绑定到网格的类有 2 列,其类型为 Nullable。当我运行代码时,视图会抛出如下错误:

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

我最初认为这可能是模板的渲染问题,该模板仅适用于 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; }
}

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

I am reasonably new to Telerik MVC extensions. I have implemented my first instance in a view successfully. I am not implementing my second view using the Telerik MVC Grid, however the class that is being bound to the grid has 2 columns which are of type Nullable. When I run my code, the view spits out an error as follows:

The model item passed into the dictionary is null, but this dictionary requires a non-null model item of type 'System.DateTime'.

I originally thought this might be a rendering problem with a template that only works with DateTime and not Nullable, but then I totally took out any columns displaying these DataTime? properties.

My code is as follows:

View code for 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; }
}

Does anybody have an idea how I can solve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

家住魔仙堡 2024-10-10 04:24:48

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

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

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

这解决了问题。

I managed to find the solution to this problem on the Telerik forums. If anybody else is experiencing this problem, check out the following thread:

http://www.telerik.com/community/forums/aspnet-mvc/grid/problem-with-nullable-datetime-property.aspx#1423387
In short, the solution is that you should have a EditorTemplates folder in your Views/Shared folder of your ASP.NET MVC project. This EditorTemplates folder is added by Telerik. In this folder there is a template view called DateTime.ascx which inherits from System.Web.Mvc.ViewUserControl. The problem is that the template is expecting a normal DateTime. To fix it, change it to expect a nullable DateTime as follows:

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

This solves the problem.

£烟消云散 2024-10-10 04:24:48

您还必须删除以前可能拥有的任何(不可为空)模板,以便使用 DateTime?被击中的模板。

System.Web.Mvc.ViewUserControl

You must also delete any (non-nullable) templates you may have had before in order for the DateTime? template to be hit.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文