Validate complex types with DataAnnotations

◇◆丶佛笑我妖孽 提交于 2019-12-10 22:20:07

问题


I've decided to use Entity Framework for O/R Mapping, and DataAnnotations for validation in my project, and I have now encountered an odd problem when trying to implement this.

This is what I've done:

I have the following entity type

Contact
*******
Int32 Id (not null, Entity Key)
Name Name (not null) 
Address Address (not null)
String Phone
String Email

where Name and Address are complex types defined as follows:

Name                                  Address
****                                  *******
String First (not null)               String Street (not null)
String Last (not null)                String ZipCode (not null)
                                      String City (not null)

And the following classes reside in the same namespace as my entities:

public class ContactMetadata
{
    [Required]
    public Name Name { get; set; }
}

[MetadataType(typeof(ContactMetadata))]
partial class Contact { }

However, when I create a new Contact item, the Name and Address types are filled with instances of Name and Address where all values are null, instead of Name and Address having null values themselves. Thus, the Required attribute doesn't throw any errors, although all values are null. How do I work around this?


回答1:


So it creates instances of the Name and Address objects who's properties are null? Interesting.

Can you just put the [Required] attribute on the children?

EDIT: I know this might be considered a smelly way of doing this, but for clarity I edit your answer into the post, so that it can easier be found by the next person having problems with this...

Suggested (and accepted, but yet untested) solution:

Write a custom validation attribute which validates against the null values.




回答2:


Make sure the names that end up in the HTML fields line up with the property names of the class.

For example, if you have this:

public class Contact {
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public Address Address { get; set; }
}

public class Address {
    public string Street { get; set; }
    public string City { get; set; }
    [...]
}

Your calls to the HTML helpers should look like this:

<%= Html.TextBox("FirstName") %>
<%= Html.TextBox("LastName") %>
<%= Html.TextBox("Address.Street") %>
<%= Html.TextBox("Address.City") %>
[...]



回答3:


Check out this blog post complex-dataannotations-validation. I think the RequiredAssociation attribute is what you need. You might have to tweak it a little bit for Entity Framework instead of LINQ to SQL.




回答4:


I'm struggling with the same issue right now. I think a semi-graceful way to do it is to reference the key primitive type as a property, and put the dataannotation on that. Here's an example with AddressID being the key field.

public class Contact{

[Required]
public int? AddressIDForValidation{
get{return this.Address.AdressID;}
}

public Address Address{get;set;}
}



public class Address{
public int? AddressID{get;set;}
public string Street{get;set;}
}


来源:https://stackoverflow.com/questions/1061561/validate-complex-types-with-dataannotations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!