RIA 服务验证

发布于 2024-10-18 07:14:27 字数 202 浏览 2 评论 0原文

我训练在客户端上验证我的实体,但它不起作用。我有“必需”以及范围和字符串长度属性。问题是只需要在客户端进行验证。我之前在客户端有 à validate 方法 我调用 SubmitChanges。但仅验证必需的属性。然后调用 SubmitChanges 并引发异常,因为我仍然存在范围或字符串长度验证错误。我正在使用 TryValidateObject:可以吗?

请帮忙:-)

I'm train to do the validation of my entity on the client, but it does not work. I've "required" and range and stringlength attributes. The problem is that only required is validating on the client side. I have à validate method on the client before
I call SubmitChanges. But only required attribute is validated against. Then SubmitChanges is called and raises an exception, because I still have range or stringlength validation errors. I'm using TryValidateObject: is it ok?

Please help :-)

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

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

发布评论

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

评论(2

远山浅 2024-10-25 07:14:27

当您调用 SubmitChanges 时,它会返回一个 SubmitOperation,它具有一个名为 HasError 的属性。如果这是真的,则意味着一个或多个实体出现错误。您还可以使用 EntitiesInError 属性,它将带您返回所有有错误的实体,包括validationErrors。

TestDomainContext tdc = new TestDomainContext();
SubmitOperation so = tdc.SubmitChanges();
if (so.HasError)
{
  foreach (Entity entity in so.EntitiesInError)
  {
      if (entity.ValidationErrors.Count() > 0)
         //Loop through validation errors to see what property is in error
  }
  so.Cancel();
}

与 TryValidate 相比,这使您可以更好地控制错误。如果有错误,您还可以取消 SubmitOperation...

When you call SubmitChanges it returns a SubmitOperation which has a property called HasError. If this is true then it means one or more of the entities are in Error. You can also use the EntitiesInError Property that will bring you back all the Entities with any errors including validationErrors

TestDomainContext tdc = new TestDomainContext();
SubmitOperation so = tdc.SubmitChanges();
if (so.HasError)
{
  foreach (Entity entity in so.EntitiesInError)
  {
      if (entity.ValidationErrors.Count() > 0)
         //Loop through validation errors to see what property is in error
  }
  so.Cancel();
}

This gives you more control over the errors than doing the TryValidate. You can also then cancel the SubmitOperation if it has errors...

娇纵 2024-10-25 07:14:27

无法进行验证,因为 TryValidateObject 仅执行“必需”验证。我们必须将最后一个参数设置为 true 来验证所有验证类型(Range Stringlength ...)。当然,在 SubmitChanges 方法中进行验证也不错,如果它已经可以在客户端进行验证,那么它就不会在服务器上进行验证。

The validation could not occur, because TryValidateObject only performs "Required" validation. We have to set the last parameter to true to validate all validation types (Range Stringlength ...). For sure validating in the SubmitChanges method is also not bad and it not go on the server for the validation if it can validate on the client-side already.

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