ASP.NET MVC 2 Numeric value validation

夙愿已清 提交于 2019-12-08 13:31:18

问题


I have this property on a class:

public virtual decimal? Number { get; set; }

When I'm using it on a form, MVC validates it automatically. If the user enters a letter, naturally an error is returned:

"The value 'D' is not valid for Number."

How do I change such error message or even control that behavior? I'm not finding the related attribute or something like that.

Thank you!


回答1:


It is actually not a message that derives from model validation. The message is added to the model state when the model binder is unable to convert an input value to the value type of the bound property. This may for example occur when the bound property is an integer and the user entered a non-numeric character in the input field of that property.

To override the message you'll unfortunately have to do it the "hard" way, i.e. extend the DefaultModelBinder class and override the SetProperty method. Here is an example:

public class MyModelBinder: DefaultModelBinder
{
    public MyModelBinder()
    {
    }

    protected override void SetProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor, object value)
    {
        string key = bindingContext.ModelName + "." + propertyDescriptor.Name;
        if (bindingContext.ModelState[key] != null)
        {

            foreach (ModelError error in bindingContext.ModelState[key].Errors)
            {
                if (IsFormatException(error.Exception))
                {
                    bindingContext.ModelState[key].Errors.Remove(error);
                    bindingContext.ModelState[key].Errors.Add(string.Format("My message for {0}.", propertyDescriptor.DisplayName));
                    break;
                }
            }
        }
        base.SetProperty(controllerContext, bindingContext, propertyDescriptor, value);
    }

    private bool IsFormatException(Exception e)
    {
        while (e != null)
        {
            if (e is FormatException)
            {
                return true;
            }
            e = e.InnerException;
        }
        return false;
    }
}



回答2:


simple use given range validator funda and you will get what you want

For any number validation you have to use different different range validation as per your requirements :

For Integer

[Range(0, int.MaxValue, ErrorMessage = "Please enter valid integer Number")]

for float

[Range(0, float.MaxValue, ErrorMessage = "Please enter valid float Number")]

for double

[Range(0, double.MaxValue, ErrorMessage = "Please enter valid doubleNumber")]


来源:https://stackoverflow.com/questions/3712261/asp-net-mvc-2-numeric-value-validation

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