2011-03-04 86 views
0

为什么MVC仍然验证此属性,因为我已经排除了它?忽略[必填]数据注释

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

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

你为什么要排除的属性?我看到你正在使用视图模型。从视图模型中排除属性的方法根本不是定义它:-) – 2011-03-04 15:16:46

回答

1

您在这里排除的ActivationCode不能绑定到您的视图模型。

但是ModelBinding仍然会验证完整的模型。

我建议创建供您使用新的视图模型,或过滤器由史蒂夫·桑德森从绑定建议here

+0

谢谢,Swaff。我刚刚读过Steve Sanderson的Pro ASP.NET MVC 2 Framework,他没有提到它。 – Eduardo 2011-03-04 16:59:45

0

不知道如果这样做完全一样的,你有什么,但我已经做了这样的事情,它已经奏效。

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

ValidateInputAttribute没有排除成员。 – Eduardo 2011-03-04 16:54:38

+0

@Eduardo我认为它是根据[这篇文章]在MVC3中添加的(http://naikvinay.wordpress.com/2010/11/08/validateinputattribute-changes-in-mvc3/)。你没有提到你使用的是哪个版本。 – 2011-03-04 17:01:38

+0

@Eduardo @Stack是正确的。直到mvc3才加入它。 – 2011-03-04 17:12:05