Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用新的错误消息创建自定义MaxLength和所需的验证,逻辑保持不变_C#_Asp.net Mvc 3 - Fatal编程技术网

C# 如何使用新的错误消息创建自定义MaxLength和所需的验证,逻辑保持不变

C# 如何使用新的错误消息创建自定义MaxLength和所需的验证,逻辑保持不变,c#,asp.net-mvc-3,C#,Asp.net Mvc 3,如何使用默认翻译的消息创建自定义必需的和最大长度验证。我可以简单地覆盖它并更改错误消息吗 我只是想写 [MaxLength(45, ErrorMessage = Translations.Attribute.MAX_LENGTH)] [Required(ErrorMessage = Translations.Attribute.REQUIRED)] 所需资源的解决方案: [MyMaxLength(45)] [MyRequired] 您应该能够仅从MaxLengthAttribute或您正在使

如何使用默认翻译的消息创建自定义
必需的
最大长度
验证。我可以简单地覆盖它并更改错误消息吗

我只是想写

[MaxLength(45, ErrorMessage = Translations.Attribute.MAX_LENGTH)]
[Required(ErrorMessage = Translations.Attribute.REQUIRED)]
所需资源的解决方案:

[MyMaxLength(45)]
[MyRequired]

您应该能够仅从
MaxLengthAttribute
或您正在使用的任何其他属性派生

public class MyRequiredAttribute : RequiredAttribute
    {
        public override string FormatErrorMessage(string name)
        {
            return string.Format("Polje {0} je obvezno", name);
        }
    }

适用于使用MVC并希望获得自定义或本地化MaxLength错误消息的任何人

1) 创建您自己的MaximumLength属性,该属性派生自使用资源文件的MaxLength

2) 创建利用MaxLength规则的MaximumLengthValidator

3) 在应用程序启动中注册MaximumLenghtAttribute和验证程序

4) 使用MaximumLength(45)属性装饰您的财产

public class MyMaxLengthAttribute : MaxLengthAttribute
{
    public MyMaxLengthAttribute(int length) : base(length)
    {
        ErrorMessage = Translations.Attribute.MAX_LENGTH;
    }

    // other ctors/members as needed
}

在本例中,我创建了从MaxLengthAttribute继承的AppMaxLength属性,MaxLength没有修复,如果需要,可以在webconfig或DBConfig上进行更改

DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(MaximumLengthAttribute), typeof(MaximumLengthValidator));
webconfig上的配置信息:

appSettings标记插入密钥

    [AppMaxLength("NotFixedCodeLength", ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "MsgMaxLength")]       
    public string Code { get; set; }


对不起,我的英语不好

您只是想本地化错误消息吗?没有比为此验证器使用ErrorMessageResourceType和ErrorMessageResourceName参数更好的选项了。这将负责服务器端验证,但不会进行任何客户端jquery验证。如果您使用的是MVCCan,您还需要创建一个MyMaxValidator。您是否需要在答案周围添加更多的上下文?
DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(MaximumLengthAttribute), typeof(MaximumLengthValidator));
public class AppMaxLengthAttribute : MaxLengthAttribute, IClientValidatable
{
    public AppMaxLengthAttribute(string configName)
    : base(int.Parse(WebConfigurationManager.AppSettings[configName]))
    {
    }

    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
    {
        var rule = new ModelClientValidationRule
        {
            ErrorMessage = FormatErrorMessage(metadata.GetDisplayName()),
            ValidationType = "maxlength"
        };
        rule.ValidationParameters.Add("max", this.Length);
        yield return rule;
    }
}
    [AppMaxLength("NotFixedCodeLength", ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "MsgMaxLength")]       
    public string Code { get; set; }
<add key="NotFixedCodeLength" value="10" />
using System.ComponentModel.DataAnnotations;        

[Required(ErrorMessage = "This field is required!!!")]
[MaxLength(30, ErrorMessage = "Max number of characters is 30 duh!")]
public string FirstName { get; set; }