在 ASP.NET MVC (3) 中使用数据注释验证时是否可以生成属性的计算值?
正如标题所说,在 ASP.NET MVC 中使用数据注释验证时是否可以生成属性的计算值(3)?
考虑信用卡的例子。信用卡必须有到期日期,通常为月和年。在用户界面中,通常最好有选择
字段供用户选择,而不是文本
字段。然而,另一方面,当将数据发送到支付网关时,它通常是多种格式的一个字符串:M/Y
; MM/YY
; MM/YYYY
;所以
我想要完成的是在验证两个单独的日期后生成最终的字符串。
无论如何,如果有人能指出我正确的方向,那就太棒了。我只使用了内置属性,所以我想当涉及到这样的自定义内容时,我想将我视为 nubski。
As the title says, is it possible to generate computed values for properties when using Data Annotation Validation in ASP.NET MVC (3)?
Consider a credit card example. The credit card must have an expiration date, usually of Month and Year. In the user interface it's generally best to have select
fields for the user to pick from instead of text
fields. On the other side however when sending the data to a payment gateway, it's usually one string in multiple formats: M/Y
; MM/YY
; MM/YYYY
; etc.
So what I want to accomplish is to generate the final string after I've validated the two individual dates.
Anyway, it would be awesome if someone can point me in the right direction. I've only used the built in attributes, so I guess treat me as a nubski when it comes to something custom like this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设您有 Month 和 Year 属性:
您的视图模型上可以有第三个属性,它代表所需的值:
因此,一旦您的视图模型成功通过验证,您就可以将 ExpirationDate 属性发送到支付网关:
Assuming you have Month and Year properties:
you could have a third property on your view model which represents the required value:
So, once your view model has successfully passed validation you could send the ExpirationDate property to the payment gateway:
添加一个不在 GUI 中显示的属性,并连接您的值:
或者您可以使用提供格式的方法
Add a property that doesnt show in the GUI, and concatenates your values:
Or you could use a method in which you supply the format