简体   繁体   English

DateTime Model Binder,适用于IE和Chrome,但不适用于Firefox

[英]DateTime Model Binder, works for IE and Chrome but not for Firefox

i have created a DefaultModelBinder to work with DateTime 我创建了一个DefaultModelBinder与DateTime一起使用

the format is "dd-MM-yyyy" 格式为“ dd-MM-yyyy”

this DefaultModelBinder works fine with IE and Chrome, 这个DefaultModelBinder与IE和Chrome兼容,

but it does not work in Firefox ?? 但在Firefox中不起作用?

any help regarding this. 关于此的任何帮助。

Update : {As Requested} 更新:{按要求}

public class DateTimeBinder : DefaultModelBinder
{
    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        var vpr = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);

        if (vpr == null)
        {
            return null;

        }

        var date = vpr.AttemptedValue;

        if (String.IsNullOrEmpty(date))
        {
            return null;
        }

        bindingContext.ModelState.SetModelValue(bindingContext.ModelName, bindingContext.ValueProvider.GetValue(bindingContext.ModelName));

        try
        {
            var realDate = DateTime.Parse(date, System.Globalization.CultureInfo.GetCultureInfoByIetfLanguageTag("en-GB"));

            bindingContext.ModelState.SetModelValue(bindingContext.ModelName, new ValueProviderResult(date, realDate.ToString("yyyy-MM-dd hh:mm:ss"), System.Globalization.CultureInfo.GetCultureInfoByIetfLanguageTag("en-GB")));

            return realDate;
        }
        catch (Exception e)
        {
            bindingContext.ModelState.AddModelError(bindingContext.ModelName, e);
            return null;
        }

    }
}

Global.asax: Global.asax:

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

Update2: [Images Added] Update2:[已添加图像]

Firefox : 火狐: Firefox图片

This is the validation Issue in Firefox. 这是Firefox中的验证问题。

Internet Explorer : IE浏览器 : Internet Explorer映像

NOTE : the request is not being sent to the SERVER, this is client-side validation from the MVC , which displays the error in FIREFOX. 注意:该请求未发送到SERVER,这是MVC的客户端验证,它在FIREFOX中显示错误。

i got the answer to this question , umm its just BROWSER is using different culture my firefox is using culture as "English (en)" , but i am unable to sort the issue totally as i dont know how to disable this error. 我得到了这个问题的答案,嗯,只是浏览器使用的是不同的文化,我的Firefox使用的是“ English(en)”,但是我无法完全解决问题,因为我不知道如何禁用此错误。

i was thinking to use a hiddenfield for the Date and then a textbox so that user's may enter the date using the Jquery UI and during the submit i can process the Date and change it to yyyy-MM-dd. 我当时想使用Date的隐藏字段,然后使用文本框,以便用户可以使用Jquery UI输入日期,并且在提交期间,我可以处理Date并将其更改为yyyy-MM-dd。

Thanks for the help guys. 感谢您的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 密码正则表达式适用于Chrome和Firefox,但不适用于IE7 - Password regex works in chrome and firefox, but not IE7 发布的网站可在Firefox / IE中运行,但不能在Chrome中运行 - Published website works in Firefox/IE but not Chrome 自定义DateTime模型联编程序不起作用 - custom DateTime model binder not working .getJSON方法在Firefox和Chrome中不起作用。 在IE中正常工作 - .getJSON method is not working in Firefox and chrome. Works fine in IE 无法访问IE中框架内的元素,但在Chrome和FireFox中可以正常工作 - Not able access elements inside frames in IE, but works fine in Chrome and FireFox 无法通过302重定向在FireFox / Chrome中读取Cookie,但可以在IE中使用 - Unable to read cookies in FireFox/Chrome via 302 redirect, but works in IE 跨浏览器AJAX和setTimeout()在IE中有效,但在Chrome / Firefox中失败 - Cross browser AJAX and setTimeout() works in IE but fails in Chrome/Firefox 自定义活页夹工作正常,但模型未更新 - Custom binder works fine, but the model is not updated 在IE9中上传文件时出现IO错误,可在IE10,Safari,Chrome和Firefox中使用 - IO Error when uploading file in IE9, works in IE10, Safari, Chrome and Firefox ASP.NET MVC模型绑定程序无法正确解析ISO DateTime - ASP.NET MVC model binder not parsing ISO DateTime correctly
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM