Asp.net mvc ASP.NET MVC-“的;验证类型名称必须是唯一的。”;
我在模型中使用ASP.NET MVC 3和数据注释,并希望从数据库中检索错误消息。所以我写了继承属性:Asp.net mvc ASP.NET MVC-“的;验证类型名称必须是唯一的。”;,asp.net-mvc,data-annotations,Asp.net Mvc,Data Annotations,我在模型中使用ASP.NET MVC 3和数据注释,并希望从数据库中检索错误消息。所以我写了继承属性: public class LocalizedRequiredAttribute : RequiredAttribute { public LocalizedRequiredAttribute(){} public override string FormatErrorMessage(string name) { return GetByKeyHelpe
public class LocalizedRequiredAttribute : RequiredAttribute
{
public LocalizedRequiredAttribute(){}
public override string FormatErrorMessage(string name)
{
return GetByKeyHelper.GetByKey(this.ErrorMessage);
}
}
public class LocalizedRegularExpressionAttribute : RegularExpressionAttribute
{
public LocalizedRegularExpressionAttribute(string pattern) : base(pattern){}
public override string FormatErrorMessage(string name)
{
return GetByKeyHelper.GetByKey(this.ErrorMessage);
}
}
我为这些属性编写了2个“适配器”,以启用客户端验证,例如:
public class LocalizedRequiredAttributeAdapter : DataAnnotationsModelValidator<LocalizedRequiredAttribute>
{
public LocalizedRequiredAttributeAdapter(ModelMetadata metadata, ControllerContext context, LocalizedRequiredAttribute attribute)
: base(metadata, context, attribute)
{
}
public static void SelfRegister()
{
DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(LocalizedRequiredAttribute), typeof(RequiredAttributeAdapter));
}
public override IEnumerable<ModelClientValidationRule> GetClientValidationRules()
{
return new[] { new ModelClientValidationRequiredRule(ErrorMessage) };
}
}
当我的模型呈现此属性的HTML时,我收到一个异常“非侵入性客户端验证规则中的验证类型名称必须唯一。以下验证类型多次出现:必需”:
[LocalizedRequired(ErrorMessage = "global_Required_AccountName")]
[LocalizedRegularExpression(User.ADAccountMask, ErrorMessage = "global_Regex_AccountName")]
public string AccountName { get; set; }
怎么了?我猜这句话可能是:
DataAnnotationsModelValidatorProvider.RegisterAdapter(
typeof(LocalizedRequiredAttribute),
typeof(RequiredAttributeAdapter));
应改为:
DataAnnotationsModelValidatorProvider.RegisterAdapter(
typeof(LocalizedRequiredAttribute),
typeof(LocalizedRequiredAttributeAdapter));
我猜这句话可能是这样写的:
DataAnnotationsModelValidatorProvider.RegisterAdapter(
typeof(LocalizedRequiredAttribute),
typeof(RequiredAttributeAdapter));
应改为:
DataAnnotationsModelValidatorProvider.RegisterAdapter(
typeof(LocalizedRequiredAttribute),
typeof(LocalizedRequiredAttributeAdapter));
这是一个类似问题的正确答案:
基本上,你不能同时拥有两个属性这是一个类似问题的正确答案:
基本上,不能同时拥有两个属性我基本上遇到了相同的问题,我用以下代码解决了这个问题:
using System;
using System.Web.Mvc;
以及验证规则:
public class RequiredIfValidationRule : ModelClientValidationRule
{
private const string Chars = "abcdefghijklmnopqrstuvwxyz";
public RequiredIfValidationRule(string errorMessage, string reqVal,
string otherProperties, string otherValues, int count)
{
var c = "";
if (count > 0)
{
var p = 0;
while (count / Math.Pow(Chars.Length, p) > Chars.Length)
p++;
while (p > 0)
{
var i = (int)(count / Math.Pow(Chars.Length, p));
c += Chars[Math.Max(i, 1) - 1];
count = count - (int)(i * Math.Pow(Chars.Length, p));
p--;
}
var ip = Math.Max(Math.Min((count) % Chars.Length, Chars.Length - 1), 0);
c += Chars[ip];
}
ErrorMessage = errorMessage;
// The following line is where i used the unique part of the name
// that was generated above.
ValidationType = "requiredif"+c;
ValidationParameters.Add("reqval", reqVal);
ValidationParameters.Add("others", otherProperties);
ValidationParameters.Add("values", otherValues);
}
}
我希望这能有所帮助。我基本上也遇到了同样的问题,我通过以下代码解决了这个问题:
using System;
using System.Web.Mvc;
以及验证规则:
public class RequiredIfValidationRule : ModelClientValidationRule
{
private const string Chars = "abcdefghijklmnopqrstuvwxyz";
public RequiredIfValidationRule(string errorMessage, string reqVal,
string otherProperties, string otherValues, int count)
{
var c = "";
if (count > 0)
{
var p = 0;
while (count / Math.Pow(Chars.Length, p) > Chars.Length)
p++;
while (p > 0)
{
var i = (int)(count / Math.Pow(Chars.Length, p));
c += Chars[Math.Max(i, 1) - 1];
count = count - (int)(i * Math.Pow(Chars.Length, p));
p--;
}
var ip = Math.Max(Math.Min((count) % Chars.Length, Chars.Length - 1), 0);
c += Chars[ip];
}
ErrorMessage = errorMessage;
// The following line is where i used the unique part of the name
// that was generated above.
ValidationType = "requiredif"+c;
ValidationParameters.Add("reqval", reqVal);
ValidationParameters.Add("others", otherProperties);
ValidationParameters.Add("values", otherValues);
}
}
我希望这有帮助。是的,你是对的,但我得到了同样的无效操作异常。是的,你是对的,但我得到了同样的无效操作异常。