在 ASP.NET MVC 3 中,当在 2 个模型属性中使用相同的复杂类型时,如何获得可变的错误消息
我遇到一个问题,我的电话号码类定义如下:
public class TelephoneNumber
{
[Required(ErrorMessage = "Phone number area code is required")]
public string AreaCode { get; set; }
[Required(ErrorMessage = "Phone number first 3 digits are required")]
public string PhoneFirst3 { get; set; }
[Required(ErrorMessage = "Phone number last 4 digits are required")]
public string PhoneLast4 { get; set; }
}
在我的模型中,我有 2 个属性,其中每个属性都使用 TelephoneNumber 数据类型。
[DisplayName("*Cell Phone")]
public TelephoneNumber CellPhone { get; set; }
[DisplayName("*Work Phone")]
public TelephoneNumber WorkPhone { get; set; }
我正在使用验证摘要,并希望修改“需要电话号码区号”中的错误消息,以包括错误消息所指的电话号码。例如“需要工作电话号码区号”和“需要手机号码区号”。
还可以添加新的电话号码类型,例如:
[DisplayName("Home Phone")]
public TelephoneNumber HomePhone { get; set; }
但不需要家庭电话吗?
这是我的实现的简化版本,但如果可以更改或设置复杂类型的属性的数据注释,以便它们可以在同一模型中包含的多个属性中配置不同的验证注释,那么创建类时会变得更加容易比简单的电话号码复杂。
谢谢。
I have a problem where I have a Telephone Number class defined as follows:
public class TelephoneNumber
{
[Required(ErrorMessage = "Phone number area code is required")]
public string AreaCode { get; set; }
[Required(ErrorMessage = "Phone number first 3 digits are required")]
public string PhoneFirst3 { get; set; }
[Required(ErrorMessage = "Phone number last 4 digits are required")]
public string PhoneLast4 { get; set; }
}
In my model I have 2 properties, where each of them use the TelephoneNumber data type.
[DisplayName("*Cell Phone")]
public TelephoneNumber CellPhone { get; set; }
[DisplayName("*Work Phone")]
public TelephoneNumber WorkPhone { get; set; }
I am using a validation summary and would like to modify the error message from "Phone number area code is required" to include which phone number the error message is referring to. Such as "Work Phone number area code is required" and "Cell Phone number area code is required".
Also is it possible to add a new phone number type such as:
[DisplayName("Home Phone")]
public TelephoneNumber HomePhone { get; set; }
But not have home phone be required?
This is a simplified version of my implementation but if it is possible to change or set data annotations for properties of complex types so they may have different validation annotations configured in multiple properties contained in the same model it would make life much easier when creating classes more complex than a simple phone number.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是设计问题吗?电话号码、工作电话号码和手机号码是不同的类,可能应该继承自抽象电话号码类。
这样您就可以根据需要注释每种类型的电话号码。我还会指定 REGEX 来验证每个数字的格式。
需要考虑的是,如果有一天我说工作电话号码应该像号码一样有分机号,会发生什么?
查找单一责任原则,这可能会有所帮助。祝你好运。
Is this a design issue? Telephone number, work telephone number and mobile are different classes that should probably inherit from abstract telephone number class.
This would then allow you to annotate each type of telephone number as you feel necessary. I would also specify REGEX for validating format of each number.
Thing to think about is what happens if one day I say that work telephone number should have extension as well as the number?
Lookup single responsibility principle, this might help. Good luck.