我对 Telerik MVC 扩展相当陌生。我已经在视图中成功实现了我的第一个实例。我没有使用 Telerik MVC 网格实现第二个视图,但是绑定到网格的类有 2 列,其类型为 Nullable。当我运行代码时,视图会输出错误,如下所示:
传递到字典中的模型项为 null,但此字典需要类型为“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; }
}
有人知道我该如何解决这个问题吗?
我设法在 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?>" %>
这解决了问题。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)