comma decimal seperator in asp.net mvc 5 [duplicate]

不羁岁月 提交于 2019-11-28 12:54:51

Your value is 3,0 which is not a valid decimal type value. It should be 3.0 replace " comma(,) with dot(.).

Edit : Create your own model binder.

public class DecimalModelBinder : DefaultModelBinder
{
    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        var valueProviderResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);

        return valueProviderResult == null ? base.BindModel(controllerContext, bindingContext) : Convert.ToDecimal(valueProviderResult.AttemptedValue);

    }    
}

Add these lines in Application_Start file.

ModelBinders.Binders.Add(typeof(decimal), new DecimalModelBinder());
ModelBinders.Binders.Add(typeof(decimal?), new DecimalModelBinder());

I think this should work now. :)

I know is old but I had the same problem (with es-AR) and I found a better solution, you can simple do this:

void Application_AcquireRequestState(object sender, EventArgs e)
{
    System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(HttpContext.Current.Session["userCulture"]);
    System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(HttpContext.Current.Session["userCulture"]);
}

On Global.asax

This code runs before model binding so you can set the culture information to the thread and also you have access to the session variable (for especific user culture)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!