未为 ModelState.Value.Culture 设置 MVC3 全球化
我已在 Action Filer 中将文化设置为
Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);
文化 = {fr-be} => 法国比利时。
仅供参考,此操作过滤器根据用户选择设置文化。
在 myAction 之一中,用户以 [dd/mm/yyyy] => 格式输入日期2011 年 7 月 26 日。 操作如下
public ActionResult RequestVacation(VacationRequest model)
{
if(ModelState.IsValid)
{
......
当我调试代码模型时。VacationDate 包含 01/01/0001 ;虽然应该是 7/26/2011 而 Form[VacationDate] 包含 26/07/2011 [采用 Fr-BE 甲酸盐] 并且 ModelState.IsValid 为 false;尽管它应该是正确的,因为 fr-be 格式的日期是正确的。 当我进一步挖掘但在视觉工作室检查当地人时 我发现
this.ModelState[1].Culture = {en-US}
我已经使用 actionFilter 设置了文化值,如上所述。 我的问题是如何设置 this.ModelState.Culture = {fr-be}?
i have set culture in Action Filer as
Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);
where culture = {fr-be} =>French Belgium.
FYI, this action filter sets the culture on user choice.
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)
{
....
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}
whereas i had already set culture value using actionFilter, as stated above.
My question is how can i set this.ModelState.Culture = {fr-be}?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
回答你的问题有点晚了,但我想发生的事情是你在操作过滤器的 OnActionExecuting 方法中设置当前线程区域性,但模型绑定发生在这之前,所以它不会接受你的更改。
在模型绑定发生之前,您需要将设置区域性的代码移动到管道中较早的位置,例如在 Application BeginRequest 方法中,或者如果需要在会话中存储区域性,则在 AcquireRequestState 中。
A bit late to answer your question but I imagine that what is happening is you are setting the current thread culture in the OnActionExecuting method of an action filter, but model binding happens prior to that so it doesn't pick up your change.
You'll need to move your code that sets the culture to earlier on in the pipeline, before model binding occurs, for example in the Application BeginRequest method, or AcquireRequestState if you need to store the culture in session.
将以下内容放入您的 web.config 中:
Put the following in your web.config:
为了回答我的上述问题,我已经以这种方式解决了它,
我知道这不是一个好的解决方案。但我仍在寻找某种方法来更改,在验证发生之前,
到
其中 {fr-BE} 是我所需的文化,以便解析日期时间。
所以我仍在寻找有人为此找到一个好的解决方案。
In response to my above question i had solved it in this way
i know this is not a good solution. but i m still looking for some way to change, before validation occurs,
To
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.