Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Convert.ToDecimal失败,并表示输入字符串的格式不正确_C#_Asp.net_Asp.net Mvc_Asp.net Mvc 3 - Fatal编程技术网

C# Convert.ToDecimal失败,并表示输入字符串的格式不正确

C# Convert.ToDecimal失败,并表示输入字符串的格式不正确,c#,asp.net,asp.net-mvc,asp.net-mvc-3,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 3,我有一个带有price属性的视图模型类 问题是,如果用户输入值$200150.90,则该值未格式化并发送到控制器 decimal的默认模型格式化程序可能有什么问题 public ItemViewModel { public string Name {get;set;} [DisplayFormat(DataFormatString = "{0:c}")] [RegularExpression(@"^\$?([0-9]{1,3},([0-9]{3},)*[0-9]{3}|[0-9]+)(.[0-

我有一个带有price属性的视图模型类

问题是,如果用户输入值
$200150.90
,则该值未格式化并发送到控制器

decimal的默认模型格式化程序可能有什么问题

public ItemViewModel
{

public string Name {get;set;}
[DisplayFormat(DataFormatString = "{0:c}")]
[RegularExpression(@"^\$?([0-9]{1,3},([0-9]{3},)*[0-9]{3}|[0-9]+)(.[0-9][0-9])?$"
ErrorMessage = "Enter a valid money value. 2 Decimals only allowed")]
public decimal? Price{ get; set; }
}
鉴于

@model ItemViewModel

@Html.TextBoxFor(m=>m.Price)
内部控制器

public ActionResult Save(ItemViewModel model)
{

 // model.Price is always null, even if it has value $200,150.90
}
我已在
Global.asax

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

   public object BindModel(ControllerContext controllerContext,
        ModelBindingContext bindingContext)
    {
        ValueProviderResult valueResult = bindingContext.ValueProvider
            .GetValue(bindingContext.ModelName);
        ModelState modelState = new ModelState { Value = valueResult };
        object actualValue = null;
        try
        {
            actualValue = Convert.ToDecimal(valueResult.AttemptedValue,
                CultureInfo.CurrentCulture);
        }
        catch (FormatException e)
        {
            modelState.Errors.Add(e);
        }

        bindingContext.ModelState.Add(bindingContext.ModelName, modelState);
        return actualValue;
    }
模型活页夹
输入字符串中的错误格式不正确

 Convert.ToDecimal("$200,150.90",CultureInfo.CurrentCulture)

如果格式为货币,则添加了额外的十进制转换

多亏了


解析过程中使用了什么区域性?@JonSkeet,检查控制器中的
System.Threading.Thread.CurrentThread.CurrentCulture
显示
en US
 string currencyDisplayFormat=(bindingContext.ModelMetadata).DisplayFormatString;
 if (!string.IsNullOrEmpty(currencyDisplayFormat) 
                           && currencyDisplayFormat == "{0:c}")
 {
    actualValue = Decimal.Parse(valueResult.AttemptedValue,
                  NumberStyles.AllowCurrencySymbol | NumberStyles.Number, 
                  CultureInfo.CurrentCulture);

 }
 else
 {
        actualValue = Convert.ToDecimal(valueResult.AttemptedValue,
                        CultureInfo.CurrentCulture);
 }