如果按 MVC5 中的函数进行验证,我该怎么做 [英] How i do Required If validation by function in MVC5

查看:22
本文介绍了如果按 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 NotifyMeByEmailstring 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆