【问题标题】:In ModelState the Date format is not valid while editing Kedno grid在 ModelState 中,日期格式在编辑剑道网格时无效
【发布时间】:2016-09-22 15:40:09
【问题描述】:

我正在使用 Kedno 网格开发 MVC 应用程序。 我有一个带有属性的模型项目:

public bool IsPaid { get; set; }
public Nullable<DateTime> PaymentDate { get; set; }

在视图中的 Kedno 网格中:

columns.Bound(c => c.PaymentDate).Format("{0:dd-MMM-yyyy}");

当我在网格中编辑并将 IsPaid 设置为 false 时,逻辑必须将 PaymentDate 设置为 null。

例如,我有一个项目的 PaymentDate 为 2016 年 9 月 22 日,IsPaid 为 true

在 Kendo 网格中编辑后,此 Item 设置 IsPaid 为 false,当我单击 Save Changes 时,它会调用控制器中的方法 Update:

public ActionResult Update([DataSourceRequest] DataSourceRequest request, [Bind(Prefix = "models")]IEnumerable<ItemViewModel> items)
{
        if (ModelState.IsValid)
        {
            //
        }
}

但 ModelState 无效:"The value '9/22/2016 12:00:00 AM' is not valid for...". 如果 PaymentDate 为 01-Sep-2016,则 ModelState 有效。

另外,我按照Globalization section 中的步骤操作。

在 web.config 中:

<globalization uiCulture="bg-BG" culture="bg-BG"></globalization>

在控制器中:

protected override void Initialize(RequestContext requestContext)
{
    Thread.CurrentThread.CurrentCulture =
        Thread.CurrentThread.CurrentUICulture =
            new CultureInfo(requestContext.HttpContext.Request.UserLanguages[0]);

    base.Initialize(requestContext);
}

在视图中:

@{
    var culture = System.Globalization.CultureInfo.CurrentCulture.ToString();
}

<script src="@Url.Content("~/scripts/cultures/kendo.culture." + culture + ".min.js")"></script>

<script>
    kendo.culture("@culture");
</script>

有什么建议吗?

非常感谢!

【问题讨论】:

  • 您似乎忘记了kendo 在您的路径中...&lt;script src="@Url.Content("~/scripts/kendo/cultures/kendo.culture." + culture + ".min.js")"&gt;&lt;/script&gt;
  • 不,脚本已正确加载。文件夹cultures 位于文件夹scripts 中。不过还是谢谢

标签: asp.net-mvc validation datetime kendo-grid currentculture


【解决方案1】:

该问题与服务器和客户端上的匹配文化无关。至于我的观察,您正确地遵循了这篇文章。

模型视图无效的原因是您将Column的Format方法设置为类似于“dd-MMM-yyyy”的格式。尝试将格式设置为:

columns.Bound(c => c.PaymentDate).Format("{0:dd/mm/yyyy}");

或者,您可以出于测试目的删除格式并检查日期是否正确传递。你可以稍后再放回去。

【讨论】:

    猜你喜欢
    • 2019-07-11
    • 1970-01-01
    • 1970-01-01
    • 2013-07-08
    • 1970-01-01
    • 1970-01-01
    • 2016-05-26
    • 1970-01-01
    • 2015-08-09
    相关资源
    最近更新 更多