Asp.net mvc xVal错误消息出现两次

Asp.net mvc xVal错误消息出现两次,asp.net-mvc,validation,data-annotations,xval,Asp.net Mvc,Validation,Data Annotations,Xval,我正在尝试使用ASP.NET MVC 2预览1项目设置xVal。我基本上是完全按照上面的例子(到目前为止,仅限于服务器端) 我已经注释了一个BlogPost实体,下面是Post操作: [HttpPost] public ActionResult Index(BlogPost b) { try { _blogService.Insert(b); } catch (RulesException ex) { ex.AddModel

我正在尝试使用ASP.NET MVC 2预览1项目设置xVal。我基本上是完全按照上面的例子(到目前为止,仅限于服务器端)

我已经注释了一个BlogPost实体,下面是Post操作:

[HttpPost]
public ActionResult Index(BlogPost b)
{
    try
    {
        _blogService.Insert(b);
    }
    catch (RulesException ex)
    {
        ex.AddModelStateErrors(ModelState, "");
    }

    return (View(b));
}
以下是服务方法:

public void Insert(BlogPost post)
{
    var errors = DataAnnotationsValidationRunner.GetErrors(post);
    if(errors.Any())
    {
        throw new RulesException(errors);
    }

    _blogRepo.Insert(post);
}
(请注意,DataAnnotationsValidationRunner是示例博客文章中的逐字逐句)。当我提交一个完全无效的BlogPost表单时,我会得到以下验证错误列表:

  • 需要一个值
  • 请输入标题
  • 请输入过账日期
  • 请输入一些内容
  • 请输入标题
  • 请输入过账日期
  • 请输入一些内容

我甚至不知道第一条消息的目的是什么,但正如您所看到的,其他错误出现了两次。我做错了什么?或者这是MVC V2的一个问题?

从ASP.Net MVC 2预览版1开始,我们现在获得了现成的DataAnnotation验证支持,因此我猜您的问题是,当ModelBinder逻辑运行时,它正在应用DataAnnotation规则:

public ActionResult Index(BlogPost b) //Create BlogPost object and apply rules
然后使用XVal逻辑再次请求检查:

var errors = DataAnnotationsValidationRunner.GetErrors(post);
事实证明,它们是以相同的顺序重复的

您的代码在MVC第1版中运行良好,因为公共操作结果索引(BlogPost b)不会运行DataAnnotation规则。如果可以关闭新的DataAnnotation逻辑并只使用XVal,我还没有读到任何地方

有更多关于这方面的信息

要了解第一项运行的是什么,请调试并检查ModelState上有哪些错误,因为这将告诉您错误与对象上的哪个属性相关

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

从ASP.Net MVC 2预览版1开始,我们现在获得了现成的DataAnnotation验证支持,因此我猜您的问题是,当ModelBinder逻辑运行时,它正在应用DataAnnotation规则:

public ActionResult Index(BlogPost b) //Create BlogPost object and apply rules
然后使用XVal逻辑再次请求检查:

var errors = DataAnnotationsValidationRunner.GetErrors(post);
事实证明,它们是以相同的顺序重复的

您的代码在MVC第1版中运行良好,因为公共操作结果索引(BlogPost b)不会运行DataAnnotation规则。如果可以关闭新的DataAnnotation逻辑并只使用XVal,我还没有读到任何地方

有更多关于这方面的信息

要了解第一项运行的是什么,请调试并检查ModelState上有哪些错误,因为这将告诉您错误与对象上的哪个属性相关

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

是的,就是这样。我真的没想到注释的东西会那么容易,哇。无论如何,另一个错误是因为我没有将Id值设置为使用私有集(nhibernatepoco)。非常感谢!是的,就是这样。我真的没想到注释的东西会那么容易,哇。无论如何,另一个错误是因为我没有将Id值设置为使用私有集(nhibernatepoco)。非常感谢!