从ASP开始。Net MVC 2 Preview 1我们现在可以开箱即用地获得DataAnnotation验证支持,所以我想你的问题是,当ModelBinder逻辑运行时,它正在应用DataAnnotation规则:
public ActionResult Index(BlogPost b) //Create BlogPost object and apply rules
var errors = DataAnnotationsValidationRunner.GetErrors(post);
这一点得到了它们以相同顺序重复的事实的支持。
你的代码在MVC版本1中运行良好,因为
公共行动结果索引(BlogPost b)
不会运行DataAnnotation规则。如果可以关闭新的DataAnnotation逻辑并仅使用XVal,我还没有在任何地方读过。
Scott's post able preview 1
[HttpPost]
public ActionResult Index(BlogPost b)
{
try
{
_blogService.Insert(b); //Add breakpoint here and check ModelState
}
catch (RulesException ex)
{
ex.AddModelStateErrors(ModelState, "");
}
return (View(b));
}