如果通过MVC5中的功能进行验证,我该怎么做 [英] How i do Required If validation by function in MVC5
问题描述
如果通过功能检查存在电子邮件,我希望显示错误
if email exist by function check i want display error
我该怎么做?
[RequiredIf(BL.datafuncs.checkIfExist(email) == true, ErrorMessage = "email already exist")]
public string email { get; set; }
推荐答案
RequiredIf
属性用于根据另一个属性的值来验证所需的属性.例如,如果您的模型包含属性bool NotifyMeByEmail
和string EmailAddess
,则可以按以下方式应用它.
The RequiredIf
attribute is for validating a property that is required based on the value of another property. For example if you model contains properties bool NotifyMeByEmail
and string EmailAddess
then you could apply it as follows.
public bool NotifyMeByEmail { get; set; }
[RequiredIf("NotifyMeByEmail", ErrorMessage = "Please enter you email address")]
public string EmailAddress { get; set; }
然后在视图中,如果未选中NotifyMeByEmail
的复选框,则会为EmailAddress
生成验证错误.
Then in the view, if the checkbox for NotifyMeByEmail
is not checked, a validation error is generated for EmailAddress
.
您似乎真的想验证用户输入的电子邮件在数据库中不存在,在这种情况下,您需要一个[Remote]
属性(标准MVC,并非万无一失). 如何:在ASP.NET MVC中实现远程验证
It looks like you actually want to validate the the email enter by the user does not already exist in he database, in which case you need a [Remote]
attribute (standard MVC, not foolproof). How to: Implement Remote Validation in ASP.NET MVC
这篇关于如果通过MVC5中的功能进行验证,我该怎么做的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!