3

私は、Telerik MVC 拡張機能を初めて使用します。ビューに最初のインスタンスを正常に実装しました。Telerik MVC Grid を使用して 2 番目のビューを実装していませんが、グリッドにバインドされているクラスには Nullable 型の 2 つの列があります。コードを実行すると、ビューは次のようなエラーを吐き出します。

ディクショナリに渡されたモデル アイテムは null ですが、このディクショナリには 'System.DateTime' 型の null 以外のモデル アイテムが必要です。

当初、これは 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; }
}

どうすればこれを解決できるか考えている人はいますか?

4

2 に答える 2

6

Telerik フォーラムでこの問題の解決策を見つけることができました。他の誰かがこの問題を経験している場合は、次のスレッドを確認してください。

http://www.telerik.com/community/forums/aspnet-mvc/grid/problem-with-nullable-datetime-property.aspx#1423387 要するに、解決策は、Views/ に EditorTemplates フォルダーを作成することです。 ASP.NET MVC プロジェクトの共有フォルダー。この EditorTemplates フォルダーは、Telerik によって追加されます。このフォルダーには、System.Web.Mvc.ViewUserControl から継承した DateTime.ascx というテンプレート ビューがあります。問題は、テンプレートが通常の DateTime を想定していることです。これを修正するには、次のように null 許容の DateTime を期待するように変更します。

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

これで問題は解決します。

于 2010-11-18T08:05:01.470 に答える
1

また、DateTime? ヒットするテンプレート。

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

于 2010-12-16T19:55:35.260 に答える