按照MVC文化DateTime格式 [英] DateTime format according to the culture in MVC

查看:174
本文介绍了按照MVC文化DateTime格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MVC视图,我列出了一个名为CreatedOn一个DATETIME类型的列,该值的格式是这样的:' DD / MM / YYYY HH:MM:SS
当我点击编辑链接来修改的值我获得同样的格式,

I have an MVC view where I list a dateTime type column named "CreatedOn", the values are formatted like this: 'DD/MM/YYYY HH:MM:SS', When I click on edit link to modify a value I obtain the same format,

当我修改编辑的值,我得到验证错误:现场CreatedOn必须是日期

When I modify the edited value I get the validation error: "The field CreatedOn must be a date."

我的EF模型类此字段映射为DateTime类型。

My EF model class maps this field as DateTime type.

我的数据库归类为 French_CI ,在SQL Server表中的字段是SMALLDATETIME类型,
当我打开表看字段格式我觉得喜欢' YYYY-MM-DD HH:MM:SS

My database collation is French_CI, the field in the sql server table is smalldatetime type, when I open the table to see the field format I find it like 'YYYY-MM-DD HH:MM:SS'

我验证默认的应用程序的文化和发现FR-FR,那么我在web.config文件中添加下一个code:

I verified the default application culture and found "fr-FR", then I added in the web.config file the next code:

<system.web>
  <globalization uiCulture="fr-FR" culture="fr-FR" />
</system.web>

但问题仍然保持不变,任何想法吗?

But the issue still remain the same, any idea please ?

推荐答案

JQuery验证不理解的文化。

jQuery Validate doesn't understand cultures.

我相信的日期,它只是调用JavaScript:新的日期(值)

I believe for dates, it just calls the javascript: new Date(value).

您可以用全球化的插件和一些JavaScript解决这个问题。

You can fix this with a globalization plugin and a bit of javascript.

微软编写了一个全球化的插件在这里: https://github.com/jquery/globalize

Microsoft wrote a globalization plugin here: https://github.com/jquery/globalize

那么你一定要像这样​​的东西来代替验证方法:

Then you want to replace the validation method with something like this:

$.validator.methods.date = function (value, element) {
  return this.optional(element) || Globalize.parseDate(value);
}

您可能还需要替换号码等其他方法 - 看看 jquery.validate.js 为例子底部

You probably also want to replace the other methods for numbers etc. - have a look at the bottom of jquery.validate.js for examples.

这篇关于按照MVC文化DateTime格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆