[英]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 : 火狐:
This is the validation Issue in Firefox. 这是Firefox中的验证问题。
Internet Explorer : IE浏览器 :
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.