0

我在 ASP.NET MVC 站点上有一个表单,我最近将它从 POST 更改为 GET。但是,当我完成此操作时,我意识到相关操作中的 DateTime 参数现在有效地使用了不同的文化 - en-US 用于 GET,而不是 en-GB 用于 POST。所以基本上日期和月份都在切换。

以下文章向我解释了为什么要这样做,但是我需要找到一种方法来克服它,而无需在提交之前恢复为 POST 或使用 JS 修改表单。

http://xhalent.wordpress.com/2011/05/14/localization-of-dates-in-asp-net-mvc/ http://weblogs.asp.net/melvynharbour/archive/2008/11/21/ mvc-modelbinder-and-localization.aspx

我为 DateTime 创建了一个自定义活页夹,只是为了试验一个理论,但这样做似乎已经解决了这个问题。谁能解释为什么这有效?

自定义活页夹基本上是 global.ascx.cs 中的以下行

ModelBinders.Binders.Add(typeof(DateTime), new DateTimeModelBinder());

这对于 DateTimeModelBinder 类

public class DateTimeModelBinder : IModelBinder
{
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        return bindingContext.Model;
    }
}
4

1 回答 1

0

看来JS是做到这一点的唯一方法。我认为上面的答案只是返回模型构造函数设置的值。

于 2012-02-10T16:09:23.590 回答