未为ModelState.Value.Culture设置MVC3全球化 [英] MVC3 globalization not set for ModelState.Value.Culture
问题描述
我已将Action Filer中的区域性设置为
i have set culture in Action Filer as
Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);
其中文化= {fr-be} =>法国比利时.
where culture = {fr-be} =>French Belgium.
仅供参考,此操作过滤器可根据用户选择设置区域性.
FYI, this action filter sets the culture on user choice.
以[dd/mm/yyyy] => 26/7/2011格式输入日期. 动作如下
in one of myAction user is entering Date in format [dd/mm/yyyy] => 26/7/2011. Action is as follows
public ActionResult RequestVacation(VacationRequest model)
{
if(ModelState.IsValid)
{
....
当我调试代码模型时.VacationDate包含01/01/0001;虽然应该是2011年7月26日 而Form [VacationDate]包含2011年7月26日[以Fr-BE形式存在] 并且ModelState.IsValid为false;尽管它应该是正确的,因为日期在fr-be格式中是正确的. 当我继续挖掘但在Visual Studio中检查当地人时 我发现了
when i dubug the code model.VacationDate contains 01/01/0001 ; although it should be 7/26/2011 whereas Form[VacationDate] contains 26/07/2011 [which is in Fr-BE formate] And ModelState.IsValid is false; although it should be true, as date is correct in fr-be format. when i furtur dig out but checking locals in visual studio i found
this.ModelState[1].Culture = {en-US}
如上所述,我已经使用actionFilter设置了区域性值. 我的问题是如何设置this.ModelState.Culture = {fr-be}?
whereas i had already set culture value using actionFilter, as stated above. My question is how can i set this.ModelState.Culture = {fr-be}?
推荐答案
针对上述问题,我已经通过这种方式解决了
In response to my above question i had solved it in this way
if (ModelState.Keys.Contains("VactionDate"))
{
ModelState err = ModelState["VactionDate"];
if (!err.Value.Culture.Equals(Thread.CurrentThread.CurrentCulture))
{
try
{
DateTime dt = Convert.ToDateTime(err.Value.AttemptedValue, Thread.CurrentThread.CurrentCulture.DateTimeFormat);
model.VactionDate = dt;
ModelState.Remove("VactionDate");
}
catch
{
}
}
}
我知道这不是一个好的解决方案.但我仍在寻找某种方式来进行验证之前更改
i know this is not a good solution. but i m still looking for some way to change, before validation occurs,
ModelState[n].Value.Culture = {en-US}
收件人
ModelState[n].Value.Culture = {fr-BE}
其中{fr-BE}是我需要的文化,用于解析dateTime. 因此,我仍在寻找某人,以为此找到一个好的解决方案.
where {fr-BE} is my required Culture, for dateTime to be parsed. so i m still looking for someone to find out a good solution for this.
这篇关于未为ModelState.Value.Culture设置MVC3全球化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!