请求:
Accept-Language:da,en;q=0.8,ru;q=0.6,en-US;q=0.4
...
Price: "120,12"
动作方法'模型包含:public double Price { get; set; }
内幕:
var uICulture = Thread.CurrentThread.CurrentUICulture; // {da}
var culture = Thread.CurrentThread.CurrentCulture; // {da-DK}
本地化由Global.asax
内的Application_AcquireRequestState
控制:
var cultureInfo = new CultureInfo("da");
Thread.CurrentThread.CurrentUICulture = cultureInfo;
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(cultureInfo.Name);
提前感谢您的帮助!
答案 0 :(得分:0)
您可以编写自定义模型装订器。
public class DecimalModelBinder : IModelBinder
{
public object BindModel(ControllerContext controllerContext,
ModelBindingContext bindingContext)
{
var valueResult = bindingContext.ValueProvider
.GetValue(bindingContext.ModelName);
var modelState = new ModelState {Value = valueResult};
object actualValue = null;
try
{
//Check if this is a nullable decimal and a null or empty string has been passed
var isNullableAndNull = (bindingContext.ModelMetadata.IsNullableValueType &&
string.IsNullOrEmpty(valueResult.AttemptedValue));
//If not nullable and null then we should try and parse the decimal
if (!isNullableAndNull)
{
actualValue = decimal.Parse(valueResult.AttemptedValue, NumberStyles.Any, CultureInfo.CurrentCulture);
}
}
catch (FormatException e)
{
modelState.Errors.Add(e);
}
bindingContext.ModelState.Add(bindingContext.ModelName, modelState);
return actualValue;
}
}
}
只需将CultureInfo.CurrentCulture更改为用户在decimal.Parse语句中选择的文化。然后在启动时注册新的模型绑定器。