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?
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.