2016-08-14 47 views
3

我尝试在我的应用程序中使用小数(ASP.NET Core在Azure中的.Net 4.5.2 Full Framework上运行)。该应用程序被配置为只使用DE-DE文化Startup.cs用自定义DateTime格式:数字格式在ASP.NET Core中不起作用

var dtf = new DateTimeFormatInfo 
     { 
      ShortDatePattern = "dd.MM.yyyy", 
      LongDatePattern = "dd.MM.yyyy HH:mm", 
      ShortTimePattern = "HH:mm", 
      LongTimePattern = "HH:mm" 
     }; 

     services.Configure<RequestLocalizationOptions>(
      options => 
      { 
       var supportedCultures = new List<CultureInfo> 
        { 
         //new CultureInfo("en-US") { DateTimeFormat = dtf }, 
         //new CultureInfo("en") { DateTimeFormat = dtf }, 
         new CultureInfo("de-DE") { DateTimeFormat = dtf }, 
         new CultureInfo("de") { DateTimeFormat = dtf } 
         //new CultureInfo("en-US"), 
         //new CultureInfo("en"), 
         //new CultureInfo("de-DE"), 
         //new CultureInfo("de") 
        }; 

       options.DefaultRequestCulture = new RequestCulture(culture: "de-DE", uiCulture: "de-DE"); 
       options.SupportedCultures = supportedCultures; 
       options.SupportedUICultures = supportedCultures; 
      }); 

我的模型看起来像这样,我还试图用{0:####}这也没有工作,以及更改类型为“小数?”

[Display(Name = "ContainerWeight", ResourceType = typeof(SharedResource))] 
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:N3}")] 
    public float? Weight { get; set; } 

如果我提交表单,如果我使用例如JavaScript验证,则会出现错误。 111,222在我的机器上,这是一个en-US Windows机器,如果我使用111.222,我的控制器中会出现模型错误。在德国的机器上,它看起来正好相反(我问别人为我检查)。这是视图的一部分:

<div class="form-group col-sm-12 col-md-6"> 
         <label asp-for="Weight" class="col-md-3 control-label"></label> 
         <div class="col-md-9"> 
          <input asp-for="Weight" class="form-control" /> 
          <span asp-validation-for="Weight" class="text-danger" /> 
         </div> 
        </div> 

我有类似麻烦的DateTime格式工作,但想通了,这个人似乎对我来说很难:-)

任何想法表示赞赏。

回答

2

根据文档,您必须使用本地化中间件来设置当前的请求文化。您必须在Configure方法中执行此操作,而不是在ConfigureService方法中执行。

我在Configure方法中做了以下事情。

var dtf = new DateTimeFormatInfo 
      { 
       ShortDatePattern = "dd.MM.yyyy", 
       LongDatePattern = "dd.MM.yyyy HH:mm", 
       ShortTimePattern = "HH:mm", 
       LongTimePattern = "HH:mm" 
      }; 
      var supportedCultures = new List<CultureInfo> 
         { 
         //new CultureInfo("en-US") { DateTimeFormat = dtf }, 
         //new CultureInfo("en") { DateTimeFormat = dtf }, 
         new CultureInfo("de-DE") { DateTimeFormat = dtf }, 
         new CultureInfo("de") { DateTimeFormat = dtf } 
         //new CultureInfo("en-US"), 
         //new CultureInfo("en"), 
         //new CultureInfo("de-DE"), 
         //new CultureInfo("de") 
        }; 

      app.UseRequestLocalization(new RequestLocalizationOptions 
      { 
       DefaultRequestCulture = new RequestCulture("de-DE"), 
       // Formatting numbers, dates, etc. 
       SupportedCultures = supportedCultures, 
       // UI strings that we have localized. 
       SupportedUICultures = supportedCultures 
      }); 

之后,我创建了示例模型。

public class TestModel 
    { 
     [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:N3}")] 
     public float? Weight { get; set; } 
    } 

并从UI传递值111,112,并在UI中以及在控制器级别成功验证。

相关问题