值类型的自定义验证消息

值类型的自定义验证消息

本文介绍了ASP.NET MVC - 值类型的自定义验证消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用的UpdateModel或TryUpdateModel,MVC框架是足够聪明,知道如果你试图在一个空传递到一个值类型(例如用户忘记填写所需的诞生日字段)。

When I use UpdateModel or TryUpdateModel, the MVC framework is smart enough to know if you are trying to pass in a null into a value type (e.g. the user forgets to fill out the required Birth Day field) .

不幸的是,我不知道如何重写默认消息,需要值。在总结成更有意义的(请输入你的生日输入)。

Unfortunately, I don't know how to override the default message, "A value is required." in the summary into something more meaningful ("Please enter in your Birth Day").

必须有这样的一种方式(无需编写太多变通code),但我不能找到它。任何帮助吗?

There has to be a way of doing this (without writing too much work-around code), but I can't find it. Any help?

修改

另外,我想这也将是无效的转换,例如一个问题生日=你好。

Also, I guess this would also be an issue for invalid conversions, e.g. BirthDay = "Hello".

推荐答案

延长DefaultModelBinder使自己的ModelBinder的:

Make your own ModelBinder by extending DefaultModelBinder:

public class LocalizationModelBinder : DefaultModelBinder

覆盖的SetProperty:

Override SetProperty:

        base.SetProperty(controllerContext, bindingContext, propertyDescriptor, value);

        foreach (var error in bindingContext.ModelState[propertyDescriptor.Name].Errors.
            Where(e => IsFormatException(e.Exception)))
        {
            if (propertyDescriptor.Attributes[typeof(TypeErrorMessageAttribute)] != null)
            {
                string errorMessage =
                    ((TypeErrorMessageAttribute)propertyDescriptor.Attributes[typeof(TypeErrorMessageAttribute)]).GetErrorMessage();
                bindingContext.ModelState[propertyDescriptor.Name].Errors.Remove(error);
                bindingContext.ModelState[propertyDescriptor.Name].Errors.Add(errorMessage);
                break;
            }
        }

添加功能布尔IsFormatException(例外五)来检查,如果一个异常出现FormatException:

Add the function bool IsFormatException(Exception e) to check if an Exception is a FormatException:

if (e == null)
            return false;
        else if (e is FormatException)
            return true;
        else
            return IsFormatException(e.InnerException);

创建一个属性类:

Create an Attribute class:

[AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = false)]
public class TypeErrorMessageAttribute : Attribute
{
    public string ErrorMessage { get; set; }
    public string ErrorMessageResourceName { get; set; }
    public Type ErrorMessageResourceType { get; set; }

    public TypeErrorMessageAttribute()
    {
    }

    public string GetErrorMessage()
    {
        PropertyInfo prop = ErrorMessageResourceType.GetProperty(ErrorMessageResourceName);
        return prop.GetValue(null, null).ToString();
    }
}

属性添加到属性希望验证:

Add the attribute to the property you wish to validate:

[TypeErrorMessage(ErrorMessageResourceName = "IsGoodType", ErrorMessageResourceType = typeof(AddLang))]
    public bool IsGood { get; set; }

AddLang是一个RESX文件和IsGoodType是资源的名称。

AddLang is a resx file and IsGoodType is the name of the resource.

最后加入到Global.asax.cs中的Application_Start这样的:

And finally add this into Global.asax.cs Application_Start:

ModelBinders.Binders.DefaultBinder = new LocalizationModelBinder();

干杯!

这篇关于ASP.NET MVC - 值类型的自定义验证消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 22:19