MVC3验证与英国的日期格式ComponentModel.DataAnnotations(也使用jQuery UI的日期选择器) [英] MVC3 Validation with ComponentModel.DataAnnotations for UK date format (also using jquery ui datepicker)

查看:139
本文介绍了MVC3验证与英国的日期格式ComponentModel.DataAnnotations(也使用jQuery UI的日期选择器)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到有这一些类似的问题,但没有解决我的问题。

我的工作与实体框架4.3的应用程序MVC3。我有计划让用户使用jQuery UI日期选择器(这是我的工作得到了感谢编辑UK日期字段设置为<一个href=\"http://blogs.msdn.com/b/stuartleeks/archive/2011/01/25/asp-net-mvc-3-integrating-with-the-jquery-ui-date-picker-and-adding-a-jquery-validate-date-range-validator.aspx\">this博客)。

幸运的是我这个博客包括使英国使用的格式说明日期选择器,但是,EF验证仍然告诉我,我需要输入一个有效的日期格式。 Wierdly它没有prevent我从提交日至DB它只是不显眼的审定踢和显示信息。

目前,我有以下数据注解:

  [数据类型(DataType.Date)
公众的System.DateTime Module_Date {搞定;组; }

但我也有尝试添加:

  [DisplayFormat(DataFormatString ={0:DD / MM / YYYY})]

这是没有效果的。我希望有人有一个解决方案,因为我不喜欢关闭不显眼的审定停止这种错误消息。

感谢

修改

以下@Iridio的答案,我看着加入模型绑定,并且确实是从像这样的,我读它似乎是做正确的事的几个帖子,但我想出了没有效果。这里是我曾尝试:

 公共类DateTimeBinder:IModelBinder
{
    公共对象BindModel(ControllerContext controllerContext,ModelBindingContext的BindingContext)
    {
        VAR值= bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
        VAR日期= value.ConvertTo(typeof运算(日期时间),CultureInfo.CurrentCulture);        归期;
    }
}

本在的Global.asax.cs 文件中的的Application_Start()方法:

  ModelBinders.Binders.Add(typeof运算(日期时间),新DateTimeBinder());
ModelBinders.Binders.Add(typeof运算(日期时间),新DateTimeBinder());


解决方案

右键,问题与JQuery验证脚本坚持使用美国datefomat。我将抑制我自己从关于世界上大多数的使用DD / MM / YYYY虽然实际上是一个正确的言论去。

不管怎样,最终我发现在注释中回答我的困境了类似的答案<一个href=\"http://stackoverflow.com/questions/7438754/localizing-jquery-validation-with-asp-net-mvc-3\">question,它的作者好心写了的博客文章他是如何解决这个问题。

基本上我已经使用了 jQuery的全球化脚本以及刚刚成立的文化 EN-GB 。我要指出,在他的博客,他没有提到放在哪里,你指定的文化一点,所以我就猛在脚本标记在页面中引用全球化下的脚本:

 &LT;脚本的src =@ Url.Content(〜/脚本/ globalize.js)TYPE =文/ JavaScript的&GT;&LT; / SCRIPT&GT;
&LT;脚本的src =@ Url.Content(〜/脚本/ globalize.culture.en-GB.js)TYPE =文/ JavaScript的&GT;&LT; / SCRIPT&GT;
&LT;脚本类型=文/ JavaScript的&GT;
    Globalize.culture(EN-GB);
    $ .validator.methods.date =功能(价值元素){
        返回this.optional(元)|| Globalize.parseDate(值);
    };
&LT; / SCRIPT&GT;

I see there are some similar questions to this, but none solve my issue.

I am working on an MVC3 app with Entity Framework 4.3. I have a UK date field that i plan to allow the user to edit using the Jquery UI datepicker (which i got working thanks to this blog).

Fortunately for me this blog includes instructions on making the datepicker using UK format, however, the EF validation is still telling me that i need to enter a valid date format. Wierdly it doesn't prevent me from submitting the date to the DB its just the unobtrusive validation kicking in and displaying the message.

At the moment I have the following data annotation:

[DataType(DataType.Date)]
public System.DateTime Module_Date { get; set; }

but i have also tried adding:

[DisplayFormat(DataFormatString="{0:dd/MM/yyyy}")]

which had no effect at all. I hope some one has a solution because i don't fancy turning off the unobtrusive validation to stop this error message.

Thanks

EDIT

following @Iridio answer, i looked into adding a Model Binding, and indeed from the few posts like this one that i read it seemed to be the right thing to do, but what i have come up with has no effect. here is what i have tried:

public class DateTimeBinder : IModelBinder
{
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
        var date = value.ConvertTo(typeof(DateTime), CultureInfo.CurrentCulture);

        return date;
    }
}

with this in the Application_Start() method of the Global.asax.cs file:

ModelBinders.Binders.Add(typeof(DateTime), new DateTimeBinder());
ModelBinders.Binders.Add(typeof(DateTime?), new DateTimeBinder());

解决方案

Right, the problem was with the jquery validate scripts insisting on using the US datefomat. I will restrain my self from going on a proper rant about the fact that the majority of the world uses dd/mm/yyyy though.

Anyway, eventually i found the answer to my woes in a comment to an answer of a similar question, the author of which kindly wrote a blog post about how he solved the issue.

Basically I have used the jquery globalize script and just set the culture to en-GB. I should mention that in his blog he doesn't mention where to put the bit where you specify the culture, so i just shoved in in script tags in the page under the references to the globalization scripts:

<script src="@Url.Content("~/Scripts/globalize.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/globalize.culture.en-GB.js")" type="text/javascript"></script>
<script type="text/javascript">
    Globalize.culture("en-GB");
    $.validator.methods.date = function (value, element) {
        return this.optional(element) || Globalize.parseDate(value);
    };
</script>

这篇关于MVC3验证与英国的日期格式ComponentModel.DataAnnotations(也使用jQuery UI的日期选择器)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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