我一直有與短日期格式結合的DateTime模特屬性相同的問題。
using System;
using System.Globalization;
using System.Web.Mvc;
namespace YourNamespaceHere
{
public class CustomDateBinder : IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
if (controllerContext == null)
throw new ArgumentNullException("controllerContext", "controllerContext is null.");
if (bindingContext == null)
throw new ArgumentNullException("bindingContext", "bindingContext is null.");
var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
if (value == null)
throw new ArgumentNullException(bindingContext.ModelName);
CultureInfo cultureInf = (CultureInfo)CultureInfo.CurrentCulture.Clone();
cultureInf.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy";
bindingContext.ModelState.SetModelValue(bindingContext.ModelName, value);
try
{
var date = value.ConvertTo(typeof(DateTime), cultureInf);
return date;
}
catch (Exception ex)
{
bindingContext.ModelState.AddModelError(bindingContext.ModelName, ex);
return null;
}
}
}
public class NullableCustomDateBinder : IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
if (controllerContext == null)
throw new ArgumentNullException("controllerContext", "controllerContext is null.");
if (bindingContext == null)
throw new ArgumentNullException("bindingContext", "bindingContext is null.");
var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
if (value == null) return null;
CultureInfo cultureInf = (CultureInfo)CultureInfo.CurrentCulture.Clone();
cultureInf.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy";
bindingContext.ModelState.SetModelValue(bindingContext.ModelName, value);
try
{
var date = value.ConvertTo(typeof(DateTime), cultureInf);
return date;
}
catch (Exception ex)
{
bindingContext.ModelState.AddModelError(bindingContext.ModelName, ex);
return null;
}
}
}
}
爲了保持與路線等,在全球ASAX文件中regiseterd我還增加了一個新的sytatic類的App_Start方式:看很多不同的例子(不僅涉及日期時間),我放在一起follwing後我MVC4項目的文件夾命名爲CustomModelBinderConfig:
using System;
using System.Web.Mvc;
namespace YourNamespaceHere
{
public static class CustomModelBindersConfig
{
public static void RegisterCustomModelBinders()
{
ModelBinders.Binders.Add(typeof(DateTime), new CustomModelBinders.CustomDateBinder());
ModelBinders.Binders.Add(typeof(DateTime?), new CustomModelBinders.NullableCustomDateBinder());
}
}
}
我然後只調用靜態RegisterCustomModelBinders從我的全球ASASX的Application_Start是這樣的:
protected void Application_Start()
{
/* bla blah bla the usual stuff and then */
CustomModelBindersConfig.RegisterCustomModelBinders();
}
這裏一個重要的註釋是,如果你一個DateTime值寫入這樣的hiddenfield:
@Html.HiddenFor(model => model.SomeDate) // a DateTime property
@Html.Hiddenfor(model => model) // a model that is of type DateTime
我這樣做,並在頁面上的實際值是格式爲「MM/DD/YYYY HH:MM: ss tt「而不是」dd/MM/yyyy hh:mm:ss tt「就像我想要的那樣。這導致我的模型驗證失敗或返回錯誤的日期(顯然交換周圍的日期和月份值)。
很多撓頭的經過和失敗的嘗試解決方案是通過在Global.asax這樣做是爲了設置爲每個請求的文化信息:
protected void Application_BeginRequest()
{
CultureInfo cInf = new CultureInfo("en-ZA", false);
// NOTE: change the culture name en-ZA to whatever culture suits your needs
cInf.DateTimeFormat.DateSeparator = "/";
cInf.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy";
cInf.DateTimeFormat.LongDatePattern = "dd/MM/yyyy hh:mm:ss tt";
System.Threading.Thread.CurrentThread.CurrentCulture = cInf;
System.Threading.Thread.CurrentThread.CurrentUICulture = cInf;
}
如果你堅持下去它不會工作Application_Start甚至是Session_Start,因爲它將它分配給會話的當前線程。正如你所知道的,Web應用程序是無狀態的,所以先前爲你的請求提供服務的線程與你當前的請求服務的線程相同,因此你的文化信息已經被傳送到數字天空中的偉大GC。
感謝去: 伊萬·勒蒂文 - http://ivanz.com/2010/11/03/custom-model-binding-using-imodelbinder-in-asp-net-mvc-two-gotchas/
garik - https://stackoverflow.com/a/2468447/578208
德米特里 - https://stackoverflow.com/a/11903896/578208
嘿..只要改變您的系統日期格式 - DD/MM/YYYY以MM/DD/YYYY和做到了。我也有同樣的問題,我通過更改系統日期格式來解決它。 – banny 2015-04-27 06:20:54
@banny如果應用程序是全球性的,可能每個人都有不同的日期時間格式,你怎麼能這樣做? ,你不應該去改變每一個日期時間格式.. – 2016-02-02 11:43:50
這些答案都沒有幫助我。表單需要進行本地化。一些用戶可能有一種方式的日期,其他方式可能有其他用戶。在web.config中設置一些東西。或global.asax不會幫助。我會繼續尋找更好的答案,但有一種方法只是將日期作爲字符串處理,直到我回到c#。 – astrosteve 2016-03-25 15:11:30