-2

您好,我正在使用 MVC傻瓜证明验证。验证我的模型,我需要使用RequiredIfNotEmpty两个字段,但我遇到了问题

模型

public class Conexionado{

    [DisplayName("Conexión")]
    [RequiredIfNotEmpty("Conex_BT2_Pos", ErrorMessage = "Error!")]
    [RequiredIfNotEmpty("Conex_BT2_N", ErrorMessage = "Conex_BT2 Cant be empty if Conex_BT2_N isnt!")]
    public string Conex_BT2 { get; set; }

    public string Conex_BT2_N { get; set; }

    [DisplayName("Ángulo BT")]
    [Range(0, 11, ErrorMessage = "Incorrect number")]
    public int? Conex_BT2_Pos { get; set; }

}

我试过一些像

[RequiredIfNotEmpty("Conex_BT2_Pos , Conex_BT2_N", ErrorMessage = "Error!")]

[RequiredIfNotEmpty("Conex_BT2_Pos || Conex_BT2_N", ErrorMessage = "Error!")]

但是在这种情况下,我可以编译,但是当我尝试使用时,Conex_BT2我得到了

'System.NullReferenceException' en FoolproofValidation.dll

有人知道我必须如何处理吗?

谢谢!

4

1 回答 1

-1

这个问题在这里得到了回答:

Stephen Muecke在同一字段上的万无一失的多个验证

Foolproof.RequiredIfNotAttribute派生自(Foolproof.ModelAwareValidationAttribute又派生自System.ComponentModel.DataAnnotation.ValidationAttribute)。ModelAwareValidationAttribute标有[AttributeUsage(AttributeTargets.Property)]参考源代码。默认情况下,AllowMultiple参数AttributeUsagefalse意味着您只能将属性应用于属性一次。您已尝试应用它 3 次,因此出现错误。

拥有它true并允许它被多次应用可能会导致设置不显眼验证所使用的$.validator.methods和函数时出现问题。$.validator.unobtrusive.adapters

您将需要使用其他一些验证属性或创建自己的ValidationAtribute实现IClientValidatable,或依赖服务器端验证。

您可以在模型属性中实现自定义验证。请参阅本教程:在 MVC 中创建自定义验证属性以创建自定义验证属性来完成您需要的工作,如果使用 IClientValidatable,您应该在其中编写自己的 jquery 验证脚本以在 MVC 框架模式下进行客户端验证,这也在其中进行了解释.

祝你好运,问候!

于 2018-04-19T19:51:02.220 回答