Reputation: 436
I try to allow decimals in my application (ASP.NET Core running on .Net 4.5.2 Full Framework in Azure). The application is configured to only use de-DE culture in Startup.cs
with a custom DateTime
format:
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;
});
My model looks like this, I also tried to use {0:#.###}
which also did not work as well as changing the type to decimal?
.
[Display(Name = "ContainerWeight", ResourceType = typeof(SharedResource))]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:N3}")]
public float? Weight { get; set; }
If I Submit my form, I get an error with JavaScript validation if I use e.g. 111,222
on my machine which is an en-US Windows machine and I get a model error in my Controller if I use 111.222
. On a german machine it's exactly the opposite as it seems (I asked someone to check that for me). This is the part of the View:
<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>
I had similar trouble getting the DateTime
format to work but figured it out, this one seems to hard for me.
Upvotes: 4
Views: 5421
Reputation: 17485
As per documentation you have to use localization middleware to set current request culture. You have to do that in the Configure
method, not in the ConfigureService
one.
I did following thing in Configure
method.
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
});
After that I created sample model.
public class TestModel
{
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:N3}")]
public float? Weight { get; set; }
}
And Pass Value 111,112
from UI and it successfully validated in UI as well at controller level as well.
Upvotes: 7