I have a model that references another model and I'd like to validate using data from the referenced model. Is this possible?
Here's my model:
namespace PHAMVC.Models {
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using ExpressiveAnnotations.Attributes;
[Table("Address")]
public partial class Address {
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int AddressID { get; set; }
public int PHAMemberID { get; set; }
[Display(Name = "Address Type (Home/Mailing/Other)")]
public int AddressTypeID { get; set; }
[Required]
[StringLength(60)]
[Display(Name = "Street")]
public string AddressLine1 { get; set; }
[StringLength(60)]
[Display(Name = " ")]
public string AddressLine2 { get; set; }
public int ZipCodeID { get; set; }
[AssertThat("(AddressTypeID == 1 && DistrictID != 0) || (AddressTypeID != 1)", ErrorMessage = "School District MUST be identified for your Home Address.")]
[Display(Name = "School District Name")]
public int DistrictID { get; set; }
public virtual AddressType AddressType { get; set; }
public virtual District District { get; set; }
public virtual ZipCode ZipCode { get; set; }
}
}
And here is the District model:
namespace PHAMVC.Models {
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Spatial;
[Table("District")]
public partial class District {
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public District() {
Addresses = new HashSet<Address>();
}
[DatabaseGenerated(DatabaseGeneratedOption.None)]
[Display(Name = "School District Name")]
public int DistrictID { get; set; }
[Required]
[StringLength(50)]
[Display(Name = "School District Name")]
public string DistrictName { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
[StringLength(4)]
public string DistrictCode { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public int? CountyNumber { get; set; }
public long? NCESDistrictID { get; set; }
[Required]
[StringLength(50)]
public string CountyName { get; set; }
[Required]
[StringLength(60)]
public string StreetAddress { get; set; }
[Required]
[StringLength(50)]
public string City { get; set; }
[Required]
[StringLength(2)]
public string StateCode { get; set; }
[StringLength(15)]
public string Phone { get; set; }
[StringLength(2)]
public string LocaleCode { get; set; }
[StringLength(25)]
public string Locale { get; set; }
public double? StudentTeacherRatio { get; set; }
public int? Students { get; set; }
public int? Teachers { get; set; }
public int? Schools { get; set; }
public int ZIP { get; set; }
public int? ZIPPlus4 { get; set; }
[StringLength(25)]
public string DistrictType { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public bool? isRegularDistrict { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public bool? isRegionalDistrict { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public bool? isStateDistrict { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public bool? isSCCounty { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
[StringLength(100)]
public string DisplayName { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Address> Addresses { get; set; }
}
}
What I'm trying to do in the Address Model, conceptually is this:
[AssertThat("ZipCodeID == District.ZIP")]
public int DistrictID { get; set; }
It seems reasonable since the District model is referenced in the Address model, but it produces this exception:
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: ExpressiveAnnotations.Analysis.ParseErrorException: Parse error on line 1, column 22:
... ZIP ...
^--- Only public properties, constants and enums are accepted. Identifier 'ZIP' not known.
Am I missing some obvious way to make District.Zip PUBLIC in the Address model? I'm really new to this, so any guidance or help would be greatly appreciated!