开发者

Ignoring [Required] data annotation

开发者 https://www.devze.com 2023-02-15 00:39 出处:网络
Why is MVC still valida开发者_如何转开发ting this property as I already excluded it? ... [Required(ErrorMessage = \"Please enter activation code\")]

Why is MVC still valida开发者_如何转开发ting this property as I already excluded it?

...
[Required(ErrorMessage = "Please enter activation code")]
public string ActivationCode { get; set; }
...

[HttpPost]
public ViewResult CreateAccount([Bind(Exclude = "ActivationCode ")] AccountCreationViewModel m, string returnUrl)
...


You are excluding the ActivationCode here from being able to be bound to your view model.

However the ModelBinding will still validate the complete model.

I would suggest creating a new ViewModel for your purpose, or a filter as suggested by Steve Sanderson here


Not sure if it does exactly the same as what you have but I have done something like this and it has worked.

[ValidateInput(true, Exclude="ActivationCode")]
[HttpPost]
public ViewResult CreateAccount(AccountCreationViewModel m, string returnUrl)
0

精彩评论

暂无评论...
验证码 换一张
取 消