Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在MVC5中按函数进行验证_C#_Asp.net Mvc 5_Foolproof Validation - Fatal编程技术网

C# 如何在MVC5中按函数进行验证

C# 如何在MVC5中按函数进行验证,c#,asp.net-mvc-5,foolproof-validation,C#,Asp.net Mvc 5,Foolproof Validation,如果通过功能检查存在电子邮件,我希望显示错误 我怎么做 [RequiredIf(BL.datafuncs.checkIfExist(email) == true, ErrorMessage = "email already exist")] public string email { get; set; } 自定义属性嵌入在程序集中,它们不是运行时的东西,因此您永远不能将函数放入属性参数中。我建议您在控制器调用中进行检查,并相应地执行一些操作属性RequiredIf用于根据另一个属性的值

如果通过功能检查存在电子邮件,我希望显示错误

我怎么做

 [RequiredIf(BL.datafuncs.checkIfExist(email) == true, ErrorMessage = "email already exist")]

 public string email { get; set; }

自定义属性嵌入在程序集中,它们不是运行时的东西,因此您永远不能将函数放入属性参数中。我建议您在控制器调用中进行检查,并相应地执行一些操作属性
RequiredIf
用于根据另一个属性的值验证所需的属性。例如,如果模型包含属性
bool NotifyMeByEmail
string EmailAddess
,则可以按如下方式应用它

public bool NotifyMeByEmail { get; set; }

[RequiredIf("NotifyMeByEmail", ErrorMessage = "Please enter you email address")]
public string EmailAddress { get; set; }
然后在视图中,如果未选中
NotifyMeByEmail
的复选框,则会为
EmailAddress
生成验证错误


看起来您实际上想要验证用户输入的电子邮件是否不存在于数据库中,在这种情况下,您需要一个
[Remote]
属性(标准MVC,不是万无一失的)

你想在这里干什么?听起来您需要一个
[Remote]
属性来检查数据库中是否已经存在电子邮件,如果已经存在,则显示错误。这不是
[RequiredIf]
的目的