Eduardo
Eduardo

Reputation: 5933

Ignoring [Required] data annotation

Why is MVC still validating 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)
...

Upvotes: 0

Views: 1684

Answers (2)

Swaff
Swaff

Reputation: 13621

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

Upvotes: 1

Bala R
Bala R

Reputation: 108957

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)

Upvotes: 0

Related Questions