2011-10-20 11 views
28

私は私のViewModelには、次のようになりますASP.NET MVC 3
を使用しています:ASP.NETのMVC3 - DateTime書式

<div class="editor-field"> 
    @Html.EditorFor(model => model.StartDate) 
    <br /> 
    @Html.ValidationMessageFor(model => model.StartDate) 
</div> 
:ビューで

public class Foo 
{ 
    [DataType(DataType.Date)] 
    [DisplayFormat(DataFormatString = "{0:dd.MM.yyyy}", ApplyFormatInEditMode = true)] 
    public DateTime StartDate { get; set; } 
    ... 
} 

、私はこのような何かを持っています

StartDateは正しい形式で表示されますが、値を19.11.2011に変更してフォームを送信すると、「'19 .11.2011 'の値がStartDateに無効です」というエラーメッセージが表示されます。

ご協力いただければ幸いです! de-DE:ドイツでのデフォルトのフォーマットだ例えば

<globalization culture="...." uiCulture="...." /> 

:あなたはdd.MM.yyyyが有効な日時形式であるためにあなたのweb.configファイルのグローバリゼーション要素に適切な文化を設定する必要が

答えて

41


UPDATE:あなたがアプリケーションのアン・米国文化を保つが、それでも日付の異なるフォーマットを使用したいコメント欄であなたの条件によると

。これは、カスタムモデルバインダーを書き込むことによって達成することができ:

Application_Startであなたを登録します
using System.Web.Mvc; 
public class MyDateTimeModelBinder : DefaultModelBinder 
{ 
    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) 
    { 
     var displayFormat = bindingContext.ModelMetadata.DisplayFormatString; 
     var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName); 

     if (!string.IsNullOrEmpty(displayFormat) && value != null) 
     { 
      DateTime date; 
      displayFormat = displayFormat.Replace("{0:", string.Empty).Replace("}", string.Empty); 
      // use the format specified in the DisplayFormat attribute to parse the date 
      if (DateTime.TryParseExact(value.AttemptedValue, displayFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out date)) 
      { 
       return date; 
      } 
      else 
      { 
       bindingContext.ModelState.AddModelError(
        bindingContext.ModelName, 
        string.Format("{0} is an invalid date format", value.AttemptedValue) 
       ); 
      } 
     } 

     return base.BindModel(controllerContext, bindingContext); 
    } 
} 

あなたのコメントに基づいて
ModelBinders.Binders.Add(typeof(DateTime), new MyDateTimeModelBinder()); 
+0

しかし、異なる日時フォーマットの英語の文化を使用することはできません。回避策はありますか? –

+0

@šljaker、はいあります。カスタムモデルのバインダーを作成し、好みの書式で日付パラメータを手動で解析する必要があります。 –

+0

グローバル化タグはタグの子です。 – encc

10

私は、あなたが望むすべてが英語の現在であることがわかりますが別の日付でフォーマット(私が間違っている場合は私を修正してください)。

事実、DefaultModelBinderは、フォームデータにサーバーのカルチャ設定を使用しています。だから私は、サーバーが "en - US"の文化を使用するが、異なる日付形式で言うことができます。

Application_BeginRequestでこれを行うことができます。完了しました。このglobal.asax.csファイル

protected void Application_BeginRequest() 
{   
    CultureInfo info = new CultureInfo(System.Threading.Thread.CurrentThread.CurrentCulture.ToString());  
    info.DateTimeFormat.ShortDatePattern = "dd.MM.yyyy"; 
    System.Threading.Thread.CurrentThread.CurrentCulture = info;  
} 

へのコードの下

protected void Application_BeginRequest() 
{ 
    CultureInfo info = new CultureInfo(System.Threading.Thread.CurrentThread.CurrentCulture.ToString()); 
    info.DateTimeFormat.ShortDatePattern = "dd.MM.yyyy"; 
    System.Threading.Thread.CurrentThread.CurrentCulture = info; 
} 

のWeb.Config

<globalization culture="en-US" /> 
+1

それはすべて私のために働いていない:( – Andrei

+0

おかげで..それは私のために働いた... –

0

追加され、web.configファイルの下に以下を追加<system.web>

<globalization culture="en-US">;