Telerik MVC Grid - problem with nullable DateTime property
Asked Answered
C

2

3

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?

Cageling answered 17/11, 2010 at 18:13 Comment(0)
C
6

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.

Cageling answered 18/11, 2010 at 8:5 Comment(3)
I'm not using anything from Telerik, but this answer still got me on the right path for the error message I was getting. Thanks!Footboy
Hi Shea I've got the same problem but I'm not using Telerik either. How did you solve your problem?Godrich
@Jacques: If you haven't solved this on your own, I ran into something similar and solved it by having both the DateTimePicker object and a DateTime object in the class, and transplanting the former to the latter after a TryUpdateModel call.Underthecounter
M
1

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>

Morrismorrison answered 16/12, 2010 at 19:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.