自定义模型绑定器 - 如何重新验证
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否尝试过
Controller
类上的TryValidateModel
方法?http://msdn.microsoft.com/ en-us/library/system.web.mvc.controller.tryvalidatemodel.aspx
Have you tried the
TryValidateModel
method on theController
class?http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.tryvalidatemodel.aspx
试试这个:
http://shazwazza .com/post/Custom-MVC-ModelBinder-with-Complex-ModelsObjects-using-built-in-MVC-Validation.aspx
try this:
http://shazwazza.com/post/Custom-MVC-ModelBinder-with-Complex-ModelsObjects-using-built-in-MVC-Validation.aspx
修复错误项后,您可以使用清除 ModelState
,然后使用重新验证
Once you have fixed the error items, you can clear the ModelState using
and then revalidate using