具有复杂类型的 IDataErrorInfo
我有一个 Address 对象,定义如下:
public class Address
{
public string StreetNumber { get; set; }
public string StreetAddress { get; set; }
public string City { get; set; }
public string PostalCode { get; set; }
}
相当简单。根据我提出的另一个问题的建议,我指的是这篇博客文章将我的 UI 数据绑定到 Person 类型的对象时(其中包含地址 MailingAddress 字段)。
问题在于 IDataError 接口方法不验证 Address 类型的任何属性。
public string this[string columnName]
{
get
{
string result = null;
// the following works fine
if(columnName == "FirstName")
{
if (string.IsNullOrEmpty(this.FirstName))
result = "First name cannot be blank.";
}
// the following does not run
// mostly because I don't know what the columnName should be
else if (columnName == "NotSureWhatToPutHere")
{
if (!Util.IsValidPostalCode(this.MailingAddress.PostalCode))
result = "Postal code is not in a know format.";
}
return result;
}
}
所以,显然我不知道 columnName 是什么......我已经逐步了解了它,它除了任何公共属性(内在类型)之外从来没有任何其他内容。我什至尝试过跑步并打破这样的说法:
if (columnName.Contains("Mailing") || columnName.Contains("Postal"))
System.Windows.Forms.MessageBox.Show(columnName);
一切都无济于事。
我有什么遗漏的吗?
I have an Address object defined simply as follows:
public class Address
{
public string StreetNumber { get; set; }
public string StreetAddress { get; set; }
public string City { get; set; }
public string PostalCode { get; set; }
}
Fairly simple. On the advice an answer to another question I asked, I am referring to this blog post when databinding my UI to an object of type Person (which contains an Address MailingAddress field).
The problem is that the IDataError interface method isn't validating any of the properties of the Address type.
public string this[string columnName]
{
get
{
string result = null;
// the following works fine
if(columnName == "FirstName")
{
if (string.IsNullOrEmpty(this.FirstName))
result = "First name cannot be blank.";
}
// the following does not run
// mostly because I don't know what the columnName should be
else if (columnName == "NotSureWhatToPutHere")
{
if (!Util.IsValidPostalCode(this.MailingAddress.PostalCode))
result = "Postal code is not in a know format.";
}
return result;
}
}
So, obviously I don't know what the columnName will be... I've stepped through it and it has never been anything other than any of the public properties (of intrinsic types). I've even tried running and breaking on a statement like:
if (columnName.Contains("Mailing") || columnName.Contains("Postal"))
System.Windows.Forms.MessageBox.Show(columnName);
All to no avail.
Is there something I'm missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要在要为其提供错误消息的所有类上定义 IErrorInfo。
You need to define IErrorInfo on all the classes that you want to supply error messages for.
看看我的回答。
这解释了如何使用 modelbinder 添加模型的“类级别”检查,而不必使用
IDataError
- 正如您在此处看到的那样,这可能非常笨拙。它仍然允许您使用 [Required] 属性或您拥有的任何其他自定义验证属性,但允许您添加或删除单个模型错误。有关如何使用数据注释的更多信息,我强烈推荐 这篇文章来自 Scott Gu。Take a look at my answer here.
This explains how to use a modelbinder to add 'class-level' checking of your model without having to use
IDataError
- which as you have seen here can be quite clumsy. It still lets you use [Required] attributes or any other custom validation attributes you have, but lets you add or remove individual model errors. For more on how to use data annotations I highly recommend this post from Scott Gu.