0

I have fields of type int?.

On view i use jquery plugin which separate textBox value with commas like this : 3,463,436 = 3463436 - it's must be int value.

But on Form subbmit i get error "The value '3,463,436' is not valid for Maximum Contiguous."

Any advice? Thanks.

demo
  • 5,676
  • 16
  • 65
  • 141

2 Answers2

1

A better solution than stripping out commas is to use .NET's built in NumberStyles parsing.

public class MyBinder : DefaultModelBinder
{
    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        string key = bindingContext.ModelName;
        var v = ((string[])bindingContext.ValueProvider.GetValue(key).RawValue)[0];
        int outPut;
        if (int.TryParse(v, NumberStyles.AllowThousands, new CultureInfo("en-US"), out outPut))
           return outPut;
        return base.BindModel(controllerContext, bindingContext);
    }
}

ModelBinders.Binders.Add(typeof(int), new MyBinder());
Erik Funkenbusch
  • 91,709
  • 28
  • 188
  • 284
0

In ASP.NET MVC one option is to implement a custom ModelBinder that strips out commas from the submitted values. I.e. implement a subclass of DefaultModelBinder

Here is how to do it for the decimal separator for the decimal data type:

How to set decimal separators in ASP.NET MVC controllers?

You will have to something similar for the thousands separator for the int data type.

Community
  • 1
  • 1
Svein Fidjestøl
  • 2,778
  • 2
  • 22
  • 39