I have Simple model with one decimal property:
public class Model {
public decimal Quantity { get;set; }
}
In my view I have:
@Html.TextBoxFor(m => m.Quantity)
@Html.ValidationMessageFor(m => m.Quantity)
The thing is that when I try to post value with decimal part (like 1.5, 2.5 etc.) I get validation errors on client or server sides depending which NumberDecimalSeparator
I use. If I post 1,5
I get client side validation error (the data-val-number one) or if I post 1.5
I get server side model validation error - "The value '1.5' is not valid for Quantity."
. I tried to set NumberDecimalSeparator
manually on Application_Start()
in Global.asax but it didn't help.
var currentCulture = (CultureInfo)CultureInfo.CurrentCulture.Clone();
currentCulture.NumberFormat.NumberDecimalSeparator = ".";
Thread.CurrentThread.CurrentCulture = currentCulture;
Thread.CurrentThread.CurrentUICulture = currentCulture;
When I manually added some values to the database and tried to edit them, the value in TextBox is displayed with dot "."
, but when I try to save another value with the dot I get server side validation error. What could be the reasons? Why didn't manual culture info update work?
// EDITS:
My approach with changing currentCulture.NumberFormat.NumberDecimalSeparator
works but only if I do it each time on Application_BeginRequest()
:
protected override void Application_BeginRequest(object sender, System.EventArgs e)
{
var currentCulture = (CultureInfo)CultureInfo.CurrentCulture.Clone();
currentCulture.NumberFormat.NumberDecimalSeparator = ".";
currentCulture.NumberFormat.NumberGroupSeparator = " ";
currentCulture.NumberFormat.CurrencyDecimalSeparator = ".";
Thread.CurrentThread.CurrentCulture = currentCulture;
Thread.CurrentThread.CurrentUICulture = currentCulture;
base.Application_BeginRequest(sender, e);
}
Why doesn't it work on application start?