如何获得自定义验证错误?
假设我有一个具有 typeid 和 salary 属性的实体 Person。我为这两个属性构建了一个业务规则,例如:
public static partial class MyRules
{
public static ValidationResult Rule1(Person p, ValidationContext context)
{
if ((p.typeid == 1) && ((p.salary == null))
{
return new ValidationResult("type 1 must should have salary",
new string[] { "Salary" });
}
return ValidationResult.Success;
}
}
代码通过 share.cs 放置在服务器端。
因此,当违反规则时,我将 SubmitOperation.HasError = true;这种错误仅在调用 SubmitChanges 后出现。并且错误不会显示在 ValidationSummary 中
,因此当 SubmitOperation.HasError = true;我如何知道 SubmitOperation 错误是验证错误而不是其他错误?当我可以将此错误识别为验证错误时,如何获取验证错误消息“类型 1 必须有薪水”并将其显示给用户?
Suppose I have entity Person with properties typeid and salary. I build a business rule for this two properties like:
public static partial class MyRules
{
public static ValidationResult Rule1(Person p, ValidationContext context)
{
if ((p.typeid == 1) && ((p.salary == null))
{
return new ValidationResult("type 1 must should have salary",
new string[] { "Salary" });
}
return ValidationResult.Success;
}
}
The code is put at server side with share.cs.
So when the rule is violated, I wil have SubmitOperation.HasError = true; This kind of error only after call SubmitChanges. and the error does not display in ValidationSummary
So when SubmitOperation.HasError = true; how can I know SubmitOperation error is validation error not other error? When I can identify this error as validation error, how can I get the validation error message "type 1 must should have salary" and show it to user?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我知道这已经快一年了;然而,没有人回答这个问题。我还没有回答任何问题,但我知道答案(或至少 1 个可能的答案)(请(并感谢)标记为已回答)。以下是我在提交操作完成时处理验证结果的方式。调用 SubmitChanges 时,您需要使用回调和用户状态来调用重载。这可能有点令人困惑,因为在回调 (InsertEntityCompleted) 中我将 op.userstate 称为回调,但这就是在此实例中使用用户状态的方式。
I know this is almost a year old; nonetheless, no one has answered it. And I have not answered any questions yet but I know the answer, (or at least 1 possible answer) (please (and thanks) mark as answered). Here is how I handle validation results on submit operations when they are completed. When calling SubmitChanges you need to call the overload with a callback and a user state. It might be a bit confusing because in the callback (InsertEntityCompleted) I call the op.userstate a callback, but thats how the user state is used in this instance.