Telerik的MVC网格 - 可空的DateTime财产问题 [英] Telerik MVC Grid - problem with nullable DateTime property

查看:125
本文介绍了Telerik的MVC网格 - 可空的DateTime财产问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有理由新Telerik的MVC扩展。我曾经在一个视图中成功实施我的第一个实例。我没有实现我使用Telerik的MVC网格第二种观点,但被绑定到网格类有2列这是可空类型的。当我运行我的code,认为吐出一个错误如下:

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:

传递到字典的模型项是空的,但本词典需要类型的非空模型项目System.DateTime的。

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

我本来以为这可能是一个渲染问题的方案,只能与DateTime和不可空的作品,但后来我完全拿出显示这些DATATIME任何列?属性。

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.

我的code是如下:

查看code为Telerik的MVC网

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

推荐答案

我设法找到对Telerik的论坛上解决这个问题。如果任何人遇到此问题,请查看以下主题:

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:

<一个href=\"http://www.telerik.com/community/forums/aspnet-mvc/grid/problem-with-nullable-datetime-property.aspx#1423387\">http://www.telerik.com/community/forums/aspnet-mvc/grid/problem-with-nullable-datetime-property.aspx#1423387
总之,解决的办法是,你应该有一个EditorTemplates文件夹中的ASP.NET MVC项目你查看/共享文件夹。这EditorTemplates文件夹由Telerik的增加。在这个文件夹中有一个名为DateTime.ascx模板视图从System.Web.Mvc.ViewUserControl继承。问题是,该模板被期待一个正常日期时间。要修复它,改变它期望一个可空日期时间如下:

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

这解决了这个问题。

这篇关于Telerik的MVC网格 - 可空的DateTime财产问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆