gpt4 book ai didi

c# - 在 CustomValidation 中返回一个复杂对象而不是 ValidationResult

转载 作者:太空宇宙 更新时间:2023-11-03 16:27:33 25 4
gpt4 key购买 nike

我有这样一个类:

[CustomValidation(typeof(Validate_Class_MetaData), "Validate")]
public class tbl_MetaData
{
[Required(ErrorMessage = "-...")]
[Range(1, Int64.MaxValue, ErrorMessage = "-...")]
public global::System.Int64 Id { set; get; }

...
}

我想使用另一个类来验证我的类:

public class Validate_Class_MetaData
{
public static ValidationResult Validate (tblData tbl, ValidationContext vc)
{

问题是我有一些错误警告。我的意思是一些业务规则不是错误,我想使用对话框向用户显示警告,以及用户是否想保存带有该警告的记录。现在我无法使用 ValidationResult 返回复杂对象。

如何同时返回 ValidationResult.Success 和一些警告?

谢谢

最佳答案

不幸的是,ValidationResult类仅支持成功或失败。来自链接(重点是我的):

If the value of the member successfully validates, the returned ValidationResult object equals the value of the Success field. To determine whether validation succeeded, you should check whether the returned object equals Success. If the value of the member does not validate, the returned ValidationResult object contains an error message and a collection of member names for the validation error, if they can be retrieved.

例如,您可以通过使用专用警告元素并向其添加文本来实现自己的警告,但 ValidationResult 不支持消息或警告。

关于c# - 在 CustomValidation 中返回一个复杂对象而不是 ValidationResult,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12071135/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com