我要如何改变在.net MVC日期时间模型绑定默认的输入格式? [英] How do I change the default input format on the DateTime model binder in .Net MVC?

查看:1047
本文介绍了我要如何改变在.net MVC日期时间模型绑定默认的输入格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个相当标准的.NET MVC控制器的方法:

I have a fairly standard .Net MVC Controller method:

public ActionResult Add(Customer cust) {  
  //do something...  
  return View();
}



如果客户是一样的东西:

Where Customer is something like:

public class Customer {
  public DateTime DateOfBirth { get; set; }
  //more stuff...
}

和包含页面

<div><%= Html.TextBox("DateOfBirth") %></div>

问题是我的网站位于美国的服务器上,以便在cust.DateOfBirth在美国解析格式为MM / DD / YYYY。不过,我希望用户输入他们在英国的格式为dd出生日期/ MM / YYYY。

The problem is my site is located on a US server so the cust.DateOfBirth is parsed in the US format MM/dd/yyyy. However, I want the users to enter their date of birth in the UK format dd/MM/yyyy.

我可以更改日期时间ModelBinder的默认输入格式或做我要创建自己的自定义ModelBinder的?

Can I change the default input format on the DateTime ModelBinder or do I have to create my own custom ModelBinder?

推荐答案

您可以改变文化在web.config文件中或在页面级别。如果你只是想更改日期格式,而不是文化的其他方面,虽然,可能需要您通过代码修改当前区域性的DateTimeFormat在Global.asax中或公共基础的控制器,并将其设置为DateTimeFormat为EN -GB。

You can change the culture in the web.config file or at the page level. If you only want to change the date format and not the other aspects of the culture, though, that may require that you modify the current culture's DateTimeFormat via code in global.asax or a common base controller and set it to the DateTimeFormat for "en-GB".

参考

要设置
的所有页面的UI文化和文化,增加一个全球化节
到Web.config文件,然后设置
的的UICulture和文化属性,如下面的示例所示

To set the UI culture and culture for all pages, add a globalization section to the Web.config file, and then set the uiculture and culture attributes, as shown in the following example:

<全球化的UICulture =EN
文化=EN-GB/>

要设置用户界面为
中的一个单独的页培养和培养,设置文化
和的UICulture属性的@页
指令,如图以下
实例

To set the UI culture and culture for an individual page, set the Culture and UICulture attributes of the @ Page directive, as shown in the following example:

<%@页的UICulture =EN
文化=EN-GB%>

要具有ASP.NET设置UI文化和
文化,那就是在当前的浏览器
设置指定设置的UICulture和文化
第一语言至
汽车。另外,您也可以设置此
值自动:culture_info_name,其中
culture_info_name是一种文化的名字。
。对于文化名的列表,请参阅
的CultureInfo。您可以进行此设置
无论是在@ Page指令或
Web.config文件

To have ASP.NET set the UI culture and culture to the first language that is specified in the current browser settings, set UICulture and Culture to auto. Alternatively, you can set this value to auto:culture_info_name, where culture_info_name is a culture name. For a list of culture names, see CultureInfo. You can make this setting either in the @ Page directive or Web.config file.

备选:

 CultureInfo.CurrentUICulture.DateTimeFormat
     = CultureInfo.CurrentCulture.DateTimeFormat
     = new CultureInfo( "en-GB", false ).DateTimeFormat;

这篇关于我要如何改变在.net MVC日期时间模型绑定默认的输入格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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