IList 属性数据注释的验证
我有类似订单的场景,其中一个订单可以有多个订单行。我在 asp.net mvc (主详细信息表单)的单页上实现此功能,我的视图模型看起来像
公共类订单,
{
public int OrderID{get;set;}
public int CustomerID{get;set;}
public DateTime OrderDate{get;set;}
public string ShippingAddress{get;set;}
[ProductUnique(ErrorMessage = "Product must be unique in an order")]
public IList<OrderLineItem> ProductLines{get;set;}
}
public class OrderLineItem
{
public int OrderLineItemID{get;set;}
public int ProductID{get;set;}
public int Quantity{get;set;}
}
我希望 ProductUnique 属性强制一个产品只能在一个订单中出现一次,我的问题是
- 是否存在一些问题 这个问题的盒子解决方案
- 如果我必须自己推出 验证属性如何
把它挂在客户端应该挂在哪里 显示错误消息(带有 属性或验证摘要中)。 如果我可以把它附在
这确实很棒,
目前我们使用的是 mvc2,但我们计划升级,因此任何 mvc3 的答案都将同样好。
谢谢
i have scenario like order form where one order can have multiple OrderLines. i am implementing this on single page in asp.net mvc (master detail form) my view Model looks like
public class Order
{
public int OrderID{get;set;}
public int CustomerID{get;set;}
public DateTime OrderDate{get;set;}
public string ShippingAddress{get;set;}
[ProductUnique(ErrorMessage = "Product must be unique in an order")]
public IList<OrderLineItem> ProductLines{get;set;}
}
public class OrderLineItem
{
public int OrderLineItemID{get;set;}
public int ProductID{get;set;}
public int Quantity{get;set;}
}
i want ProductUnique attribute to enforce that one product could appear only once in one order my questions are
- is there some out of the box solution to this problem
- if i have to roll my own
validation attribute how can i
hook it on client side where should
the error message be displayed (with
property or in validation summary).
if i can have it attached with
property that would be really great
currently we are on mvc2 but we are planning to upgrade so any answer with mvc3 would be equally good.
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这有点离题,但我认为验证不是正确的方法。为什么不让你的模型看起来像这样:
然后,如果你的用户将现有产品添加到订单中,只需显示当前数量并提供增加它的选项。这样,您就可以引导用户正确使用您的界面,而不是让他们犯错误,然后用验证消息惩罚他们。
This is a little tangential but I'm thinking validation is not the right approach here. Why not make your model look like this:
Then, if your user adds an existing product to an order, just present the current quantity and offer options for incrementing it. This way, you're guiding the user into using your interface correctly as opposed to letting them make a mistake and then punishing them with a validation message.