MVC:覆盖默认的ValidationMessage

ETF*_*fax 17 asp.net-mvc html-helper data-annotations

在MVC的世界里,我有这个视图模型......

public class MyViewModel{

[Required]
public string FirstName{ get; set; }    }
Run Code Online (Sandbox Code Playgroud)

......在我看来......这种事情......

<%= Html.ValidationSummary("Please correct the errors and try again.") %>
<%= Html.TextBox("FirstName") %>
<%= Html.ValidationMessage("FirstName", "*") %>
Run Code Online (Sandbox Code Playgroud)

我的问题:如果我提交此表单而不提供名称,我会收到以下消息"FirstName字段是必需的"

好.所以,我去改变我的财产......

[DisplayName("First Name")]
[Required]
public string FirstName{ get; set; }    
Run Code Online (Sandbox Code Playgroud)

..现在得到"名字字段是必需的"

到目前为止都很好.

所以现在我想要错误信息显示"名字Blah Blah".如何覆盖默认消息以显示DisplayName +"Blah Blah",而不是用类似的东西注释所有属性

[Required(ErrorMessage = "First Name Blah Blah")]
Run Code Online (Sandbox Code Playgroud)

干杯,

ETFairfax

cka*_*ass 16

public class GenericRequired: RequiredAttribute
{
    public GenericRequired()
    {
        this.ErrorMessage = "{0} Blah blah"; 
    }
}
Run Code Online (Sandbox Code Playgroud)

  • **它将打破jQuery验证不引人注意.** (3认同)
  • @towpse - 看看我的回答是否有帮助:http://stackoverflow.com/a/9914117/97154 (2认同)

Ant*_*lls 13

这是一种没有子类化的方法RequiredAttribute.只需制作一些属性适配器类.这里我使用ErrorMessageResourceType/ ErrorMessageResourceName(使用资源),但您可以ErrorMessage在设置之前轻松设置,甚至检查是否存在覆盖.

的Global.asax.cs:

public class MvcApplication : HttpApplication {
    protected void Application_Start() {
        // other stuff here ...
        DataAnnotationsModelValidatorProvider.RegisterAdapter(
            typeof(RequiredAttribute), typeof(CustomRequiredAttributeAdapter));
        DataAnnotationsModelValidatorProvider.RegisterAdapter(
            typeof(StringLengthAttribute), typeof(CustomStringLengthAttributeAdapter));
    }
}

private class CustomRequiredAttributeAdapter : RequiredAttributeAdapter {
    public CustomRequiredAttributeAdapter(ModelMetadata metadata, ControllerContext context, RequiredAttribute attribute)
        : base(metadata, context, attribute)
    {
        attribute.ErrorMessageResourceType = typeof(Resources);
        attribute.ErrorMessageResourceName = "ValRequired";
    }
}

private class CustomStringLengthAttributeAdapter : StringLengthAttributeAdapter {
    public CustomStringLengthAttributeAdapter(ModelMetadata metadata, ControllerContext context, StringLengthAttribute attribute)
        : base(metadata, context, attribute)
    {
        attribute.ErrorMessageResourceType = typeof(Resources);
        attribute.ErrorMessageResourceName = "ValStringLength";
    }
}
Run Code Online (Sandbox Code Playgroud)

此示例将使您创建名为Resources.resx的资源文件ValRequired,其中包含新的必需缺省消息以及ValStringLength字符串长度超过缺省消息.请注意,对于两者,{0}都会收到您可以设置的字段名称[Display(Name = "Field Name")].


Cha*_*tes 12

似乎RequiredAttribute没有实现IClientValidatable,因此如果覆盖RequiredAttribute,它会破坏客户端验证.

所以这就是我所做的,它的工作原理.希望这有助于某人.

public class CustomRequiredAttribute : RequiredAttribute, IClientValidatable
{
    public CustomRequiredAttribute()
    {
        this.ErrorMessage = "whatever your error message is";
    }

    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
    {
        yield return new ModelClientValidationRule
        {
            ErrorMessage = this.ErrorMessage,
            ValidationType = "required"
        };
    }
}
Run Code Online (Sandbox Code Playgroud)