ASP.Net MVC模型绑定 - 如何更改日期格式? [英] ASP.Net MVC model binding - how to change date format?

查看:162
本文介绍了ASP.Net MVC模型绑定 - 如何更改日期格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的应用程序,默认的日期格式必须为 DD / MM / YYYY (语言为巴西葡萄牙语)。

I have this application, and the default date format must be dd/MM/yyyy (the language is Brazilian Portuguese).

我已经设置了文化和UI文化 PT-BR 现在 myDate.ToShortDateString()返回日期,因为我想。我没有问题显示它们。

I already had set culture and UI culture to pt-BR and now myDate.ToShortDateString() returns the dates as I want. I have no trouble displaying them.

的问题是,当用户填充具有日期输入字段如 17/08/2011 和提交日期时间参数到我的动作变为零的形式。如果我的格式提供一个日期 2011年8月17日,它工作正常。

The problem is, when the user fills an input field with a date such as 17/08/2011 and submits the form the DateTime parameter to my action becomes null. If I supply a date in the format 08/17/2011, it works fine.

如何使ASP.Net MVC模型绑定正确解析我的日期?

How can I make the ASP.Net MVC model binding to parse my dates correctly?

推荐答案

我发现了什么happended。我的形式通过 GET发布方法,以及MVC只是使用的培养时,在的RouteData 或通过 POST 方法。

I found what happended. My form was posting via GET method, and the MVC just uses the culture for an action parameter when it's passed in the RouteData or by the form via POST method.

我只是改变了形式 POST 方法和它的工作。

I just changed the form to POST method and it worked.

这篇关于ASP.Net MVC模型绑定 - 如何更改日期格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆