自定义模型绑定器 - 如何重新验证

发布于 2024-09-24 09:52:25 字数 1057 浏览 8 评论 0原文

我正在使用 ASP.NET MVC 2,并且想要弄清楚如何在使用自定义绑定器填充模型后重新触发模型验证。

因此,我从几个相关的 EF 类开始,即 Booking 和 Traveler(每个预订可以有一个或多个旅行者)。

这是我用来在 Booking 上进行平衡的伙伴类:

[MetadataType(typeof(Booking_Validation))]
public partial class Booking {
    // partial class compiled with code produced by VS designer
}

[Bind(Include="Name")]
public class Booking_Validation {

    [Required(ErrorMessage="Booking name required")]
    public string Name { get; set; }

    [AtLeastOneTraveller(ErrorMessage="Please enter at least one traveller")]
    public EntityCollection<Traveller> Travellers;

}

public class AtLeastOneTraveller : ValidationAttribute {        
    public override bool IsValid(object value) {
        if (value != null) 
            return ((EntityCollection<Traveller>)value).Count > 0;
        return true;            
    }
}

我使用自定义模型绑定器来填充预订及其关联的旅行者,除了 ModelState.IsValid 似乎甚至在我的自定义模型绑定器有机会将旅行者添加到预订对象之前就已设置,即使这样做之后,ModelState["Travellers"] 仍然包含验证错误提示必须至少有一名旅客。

在自定义模型绑定器完成其工作后,有什么方法可以重新触发验证吗?

I'm using ASP.NET MVC 2 and want to figure out how to re-trigger the validation on my model after it has been populated using a custom binder.

So, I start with a couple of EF classes which are associated, Booking and Traveller (each booking can have one or more travellers)

Here's the buddy class I'm using to place balidation on Booking:

[MetadataType(typeof(Booking_Validation))]
public partial class Booking {
    // partial class compiled with code produced by VS designer
}

[Bind(Include="Name")]
public class Booking_Validation {

    [Required(ErrorMessage="Booking name required")]
    public string Name { get; set; }

    [AtLeastOneTraveller(ErrorMessage="Please enter at least one traveller")]
    public EntityCollection<Traveller> Travellers;

}

public class AtLeastOneTraveller : ValidationAttribute {        
    public override bool IsValid(object value) {
        if (value != null) 
            return ((EntityCollection<Traveller>)value).Count > 0;
        return true;            
    }
}

I use a custom model binder to populate the booking and it's associated travellers, except that ModelState.IsValid seems to be set even before my custom model binder has had a chance to add the travellers to the booking object, even even after doing so, ModelState["Travellers"] still contains the validation error saying there must be at least one traveller attached.

Is there any way to re-trigger validation after the custom model binder has done its thing?

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

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

发布评论

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

评论(3

明月夜 2024-10-01 09:52:25

您是否尝试过 Controller 类上的 TryValidateModel 方法?

http://msdn.microsoft.com/ en-us/library/system.web.mvc.controller.tryvalidatemodel.aspx

Have you tried the TryValidateModel method on the Controller class?

http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.tryvalidatemodel.aspx

梦明 2024-10-01 09:52:25

修复错误项后,您可以使用清除 ModelState

ModelState.Clear();

,然后使用重新验证

ModelState.IsValid

Once you have fixed the error items, you can clear the ModelState using

ModelState.Clear();

and then revalidate using

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