在MVC Gridview中验证日期

时间:2016-12-27 15:34:52

标签: c# asp.net-mvc-3 devexpress

我有以下gridview代码,我正在尝试对我的日期进行一些基本验证。正如您所看到的,我有一个开始日期和结束日期,我需要对它们进行验证,以便StartDate< = EndDate。有没有一种简单的方法可以将此验证添加到下面的代码中?

Html.DevExpress().GridView(settings =>
{
    settings.Name = "gvActiveScheduledAnnouncements";
    settings.Width = Unit.Percentage(100);
    settings.KeyFieldName = "AnnouncementId";

    settings.CallbackRouteValues = new { Controller = "Home", Action = "ActiveScheduledAnnouncementsPartial" };

    settings.SettingsEditing.Mode = GridViewEditingMode.EditForm;

    settings.SettingsEditing.AddNewRowRouteValues = new { Controller = "Home", Action = "AddNewAnnouncement" };
    settings.SettingsEditing.UpdateRowRouteValues = new { Controller = "Home", Action = "UpdateActiveScheduledAnnouncement" };
    settings.SettingsEditing.DeleteRowRouteValues = new { Controller = "Home", Action = "DeleteActiveScheduledAnnouncement" };

    settings.Columns.Add(c =>
    {
        c.Caption = "Announcement Text";
        c.FieldName = "Text";

        c.ColumnType = MVCxGridViewColumnType.Memo;
        c.EditFormSettings.ColumnSpan = 2;

        c.ColumnType = MVCxGridViewColumnType.Memo;
        var memo = c.PropertiesEdit as MemoProperties;
        memo.Rows = 3;
    });

    settings.Columns.Add(c =>
    {
        c.Caption = "Start Date";
        c.FieldName = "StartDate";


        c.ColumnType = MVCxGridViewColumnType.DateEdit;
    });

    settings.Columns.Add(c =>
    {
        c.Caption = "End Date";
        c.FieldName = "EndDate";

        c.ColumnType = MVCxGridViewColumnType.DateEdit;
    });

    settings.Columns.Add(c =>
    {
        c.Caption = "Display Status";
        c.FieldName = "DisplayStatus";

        c.EditFormSettings.Visible = DefaultBoolean.False;
    });
})
.Bind(Model)
.Render();

2 个答案:

答案 0 :(得分:0)

验证可以使用原始数据或使用razor或js从视图中的控制器完成。

我建议您在驱动程序中执行此操作,以便在视图中进行渲染。

答案 1 :(得分:0)

您可以采用的一种方法是启用自定义验证设置。以下链接是一种方法:Link

另一种方法是在编辑过程中通过jQuery或JavaScript手动检查该字段。在执行上述任一操作时,只需观察编辑过程何时开始,然后通过jQuery找到要监视的控件。在点击保存时(或者甚至在添加第二个日期(开始或结束)时)检查它们的值,并在用户无效时提醒用户。