我尝试在我的应用程序中使用小数(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格式工作,但想通了,这个人似乎对我来说很难:-)
任何想法表示赞赏。