Validation 验证所有字符串

Validation 验证所有字符串,validation,asp.net-mvc-4,Validation,Asp.net Mvc 4,我想禁止对提交到我的网站的所有字符串使用|字符,但我不想对每个字符串属性应用验证器属性,因为它是不可管理的 我可以验证模型绑定器中的所有字符串(我目前正在使用一个绑定器来修剪所有字符串),但我认为这不会与标准验证框架集成。i、 e.生成客户端验证 有没有办法做到这一点?我没有在客户端实现这一点,但是我可以在自定义模型活页夹中捕获所有不可靠的字符串,并使用以下命令使表单无效 public class CustomStringModelBinder : IModelBinder { publ

我想禁止对提交到我的网站的所有字符串使用|字符,但我不想对每个字符串属性应用验证器属性,因为它是不可管理的

我可以验证模型绑定器中的所有字符串(我目前正在使用一个绑定器来修剪所有字符串),但我认为这不会与标准验证框架集成。i、 e.生成客户端验证


有没有办法做到这一点?

我没有在客户端实现这一点,但是我可以在自定义模型活页夹中捕获所有不可靠的字符串,并使用以下命令使表单无效

public class CustomStringModelBinder : IModelBinder
{
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        ValueProviderResult valueResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);

        if (valueResult == null || string.IsNullOrEmpty(valueResult.AttemptedValue))
        {
            return null;
        }

        if (valueResult.AttemptedValue.Contains("|"))
        {
            bindingContext.ModelState.AddModelError(bindingContext.ModelName, "The | character is prohibited.");
        }

        return valueResult.AttemptedValue.Trim();
    }
}