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.

2

There are 2 answers

0
Svein Fidjestøl On BEST ANSWER

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.

0
Erik Funkenbusch On

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());