如何强制 ValidationAttribute 将指定的对象成员标记为无效?
我的模型包含一些成员:
public class Address
{
public Street { get; set;}
public City { get; set; }
public PostalCode { get; set; }
}
现在我的 ValidationAttribute
具有 IsValid
方法,如下所示:
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
var input = value as Address;
if (string.IsNullOrEmpty(input.City))
return new ValidationResult("City is required);
if (!string.IsNullOrEmpty(input.PostalCode))
if (string.IsNullOrEmpty(input.Street))
return new ValidationResult("Stret is required");
return ValidationResult.Success;
}
问题是:
验证我的模型后state 仅将模型错误添加到整个 Adress 成员,但我需要将其添加到城市或街道等指定成员。
对此的任何帮助将不胜感激...谢谢!
I've got my model which contains some members:
public class Address
{
public Street { get; set;}
public City { get; set; }
public PostalCode { get; set; }
}
Now I've got my ValidationAttribute
with IsValid
method overrided like this:
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
var input = value as Address;
if (string.IsNullOrEmpty(input.City))
return new ValidationResult("City is required);
if (!string.IsNullOrEmpty(input.PostalCode))
if (string.IsNullOrEmpty(input.Street))
return new ValidationResult("Stret is required");
return ValidationResult.Success;
}
The problem is:
After validation my model state adds model error only to whole Adress member, but I need it to be added to specified members like city or street.
Any help with this will be appreciated... thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以添加成员姓名!
编辑:
我已经测试并找到了解决方案:System.ComponentModel.IDataErrorInfo!
这在 javascript 客户端上不起作用,但是在提交结果时我们尝试了:)
所以继续使用 ValidationResult 和 memberNames
并且:
现在当前属性被标记为错误,memberNames 也是如此!
:D
编辑2
事实上,你可以保留简单的DataAnnotations来通知客户,
在服务器端,您可以添加更复杂的业务验证:
请注意,只有当 DataAnnotations 都有效时才调用 Validate 方法!
you can add memberNames !
EDIT :
i've tested and found a solution : System.ComponentModel.IDataErrorInfo !!
this doesn't work on the javascript client, but when submitting the result is what we attempted :)
so keep using the ValidationResult with memberNames
and :
and now the current property is marked in error, and the memberNames too !
:D
Edit 2
In fact, you can keep simple DataAnnotations to inform client,
and server side, you can add more complex business validation :
note that the Validate method is called only when DataAnnotations are all valid !
您需要使用
ValidationResult
的 this 构造函数班级。例如
You need to use this constructor of
ValidationResult
class.Eg