MVC 1 参数绑定
我正在以不变区域性将日期传递到我的服务器,格式如下
'mm/dd/yy'
MVC 中的参数绑定无法解析此日期并为参数返回 null。这可能是因为 IIS 运行在使用英语文化的计算机上(“dd/mm/yy”工作正常)。
我想覆盖服务器上所有日期的解析,以像这样使用不变文化...
Convert.ChangeType('12/31/11', typeof(DateTime), CultureInfo.InvariantCulture);
即使日期是另一个对象的一部分...
public class MyObj
{
public DateTime Date { get; set; }
}
我的控制器方法是这样的...
public ActionResult DoSomethingImportant(MyObj obj)
{
// use the really important date here
DoSomethingWithTheDate(obj.Date);
}
日期发送为像这样的 Json 数据....
myobj.Date = '12/31/11'
我尝试将 IModelBinder 的实现添加到 global.asax 中的 binderDictionary 中,
binderDictionary.Add(typeof(DateTime), new DateTimeModelBinder());
但这不起作用,而且
ModelBinders.Binders.Add(typeof(DateTime), new DataTimeModelBinder());
这似乎有些人一直想要这样做。我不明白为什么你会在服务器上的当前文化中解析日期等。客户端必须找出服务器的文化,以便格式化服务器能够解析的日期......
任何帮助表示赞赏!
I'm passing a date to my server in Invariant Culture, the following format
'mm/dd/yy'
The parameter binding in MVC fails to parse this date and returns null for the parameter. This is persumably because IIS is running on a machine using English culture ('dd/mm/yy' works just fine).
I want to override the parsing of all date on my server to use Invariant Culture like so...
Convert.ChangeType('12/31/11', typeof(DateTime), CultureInfo.InvariantCulture);
even when the date is part of another object...
public class MyObj
{
public DateTime Date { get; set; }
}
My controller method is something like this....
public ActionResult DoSomethingImportant(MyObj obj)
{
// use the really important date here
DoSomethingWithTheDate(obj.Date);
}
The date is being sent as Json data like so....
myobj.Date = '12/31/11'
I've tried adding an implementation of IModelBinder to the binderDictionary in the global.asax
binderDictionary.Add(typeof(DateTime), new DateTimeModelBinder());
That doesn't work, and neither does
ModelBinders.Binders.Add(typeof(DateTime), new DataTimeModelBinder());
This seems like some ppl would want to do all the time. I can't see why you would parse dates etc. in the current culture on the server. A client would have to find out the culture of the server just to format dates the server will be able to parse.....
Any help appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我已经解决了这里的问题,我错过的是在对象中,日期时间可以为空,
因此我的活页夹没有被拾取。
如果有人感兴趣,这就是我所做的......
在 global.asax 添加了以下内容
像这样创建一个不变的绑定器
希望这对其他人有用......
I've solved the problem here, what I had missed was that in the object, the datetime was nullable
Hence my binder wasn't being picked up.
If anyone is interested, this is what I did....
In the global.asax added the following
Created an invariant binder like so
Hope this comes in handy for someone else.....
您的问题是您的自定义模型绑定程序无法解析某些输入日期或者您的自定义模型绑定程序永远不会被调用吗?如果是前者,那么尝试使用用户浏览器的文化可能会有所帮助。
...
Is your problem that your custom model binder is unable to parse some of the input dates or that your custom model binder never gets called? If it's the former then trying to use the culture of the user's browser might help.
...
是否可以以 ISO 8601 格式将日期传递到服务器?我认为服务器会正确解析它,无论其自己的区域设置如何。
Is it possible to pass the date to the server in ISO 8601 format? I think the server would parse that correctly regardless of its own regional settings.