xVal 错误消息出现两次

发布于 2024-07-29 22:37:15 字数 1118 浏览 2 评论 0原文

我正在尝试使用 ASP.NET MVC 2 Preview 1 项目设置 xVal。 我基本上遵循 http://blog.codeville.net/2009/01/10/xval-a-validation-framework-for-aspnet-mvc/ 信中(仅限服务器端,到目前为止)。

我已经注释了 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 的问题?

I'm trying to setup xVal with an ASP.NET MVC 2 Preview 1 project. I'm basically following the example at http://blog.codeville.net/2009/01/10/xval-a-validation-framework-for-aspnet-mvc/ to the letter (server-side only, so far).

I have annotated a BlogPost entity, and here is the Post action:

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

    return (View(b));
}

And here's the service method:

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

    _blogRepo.Insert(post);
}

(Note that the DataAnnotationsValidationRunner is verbatim from the example blog post). When I submit a totally invalid BlogPost form, I get this list of validation errors:

  • A value is required.
  • Please enter a title
  • Please enter a posted date
  • Please enter some content
  • Please enter a title
  • Please enter a posted date
  • Please enter some content

I don't even know what the first message is for, but as you can see, the other errors are appearing twice. What am I doing wrong? Or is this a problem with MVC V2?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧时光的容颜 2024-08-05 22:37:16

从 ASP.Net MVC 2 Preview 1 开始,我们现在获得开箱即用的 DataAnnotation 验证支持,所以我猜您的问题是,当 ModelBinder 逻辑运行时,它正在应用 DataAnnotation 规则:

public ActionResult Index(BlogPost b) //Create BlogPost object and apply rules

然后使用您的 XVal 逻辑请求检查再次:

var errors = DataAnnotationsValidationRunner.GetErrors(post);

这得到了它们以相同顺序重复的事实的支持。

您的代码在 MVC 版本 1 中可以正常工作,因为 public ActionResult Index(BlogPost b) 不会运行 DataAnnotation 规则。 我没有读过任何地方是否可以关闭新的 DataAnnotation 逻辑并仅使用 XVal。

有关此内容的更多信息,请参见 Scott 的可发布预览 1

要找出第一个项目,请运行调试并检查 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));
}

Starting in ASP.Net MVC 2 Preview 1 we now get DataAnnotation validation support out of the box, so I guess your issue is that when the ModelBinder logic runs it is applying the DataAnnotation rules:

public ActionResult Index(BlogPost b) //Create BlogPost object and apply rules

and then with your XVal logic you are requesting the check again:

var errors = DataAnnotationsValidationRunner.GetErrors(post);

This is backed up by the fact they are repeated in the same order.

Your code would have worked fine in version 1 of MVC as public ActionResult Index(BlogPost b) would not have run the DataAnnotation rules. I have not read anywhere if it is possible to turn off the new DataAnnotation logic and just use XVal.

There is more information about this on Scott's post able preview 1

To find out what the first item is run debug and check what errors are on the ModelState, as this will tell you what property on the object the errors are related to.

[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));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文