Canvas
Canvas

Reputation: 5897

c# custom model binder for specific model and specific variable type

I have myself a problem with nullable ints and validation text.

Basically I want to change the validation message which is shown when a nullable int is not provided

so from

"The value 'xxxxxxxxxxxxxxxxxxxx' is invalid" 

to

"Please provide a valid number"

I have myself a custom model binder like so

public class IntModelBinder : IModelBinder
    {
        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            var integerValue = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
            if (integerValue == null || integerValue.AttemptedValue == "")
                return null;

            var integer = integerValue.AttemptedValue;

            bindingContext.ModelState.SetModelValue(bindingContext.ModelName, bindingContext.ValueProvider.GetValue(bindingContext.ModelName));
            try
            {
                return int.Parse(integer);
            }
            catch (Exception)
            {
                bindingContext.ModelState.AddModelError(bindingContext.ModelName, String.Format("\"{0}\" is invalid, please provide a valid number.", bindingContext.ModelName));
                return null;
            }
        }
    }

now at the moment I have updated my global.asax.cs so all nullable ints use this model binder, however I don't want all nullable ints to use this, I just want a specific model to use it and only use my model binder on the nullable ints in that model. Is there a way I can bind this modelbinder to my model and only associate with the nullable int variables?

I have tried to use my modebinder on my model like so

[ModelBinder(typeof(IntModelBinder))]
public class CreateQuoteModel
{
    ....
}

But it doesn't check on the nullable ints, I would like to avoid third party addons

Upvotes: 1

Views: 587

Answers (2)

Antony Koch
Antony Koch

Reputation: 2053

Surely so long as you have a nullable int on your model and the required attribute with a custom message this would work?

Conversely you could use a regex match which checks for length and type

Upvotes: 1

oskr
oskr

Reputation: 189

You are returning null on nullable ints

if (integerValue == null || integerValue.AttemptedValue == "")
   return null;

so you are not adding that error to your modelsatate on nullable ints.
Also I suggest you to use

int result=0;
if(!int.TryParse(integer, out result)){
   bindingContext.ModelState.AddModelError(bindingContext.ModelName, String.Format("\"{0}\" is invalid, please provide a valid number.", bindingContext.ModelName));
   return null;
}
return result;

instead of your Exception handling flow to avoid this anti-pattern

Upvotes: 1

Related Questions