How i do Required If validation by function in MVC5

懵懂的女人 提交于 2019-12-01 14:51:56

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; }

Then in the view, if the checkbox for NotifyMeByEmail is not checked, a validation error is generated for EmailAddress.

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

Custom attributes are embedded in the assembly, they are not run time stuff, so you can NEVER put a function inside the attribute argument. I would suggest you do the check in your controller call and perform some action accordingly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!