根据引用模型中的字段验证模型中的字段

时间:2016-12-21 17:22:54

标签: asp.net-mvc asp.net-mvc-5

我有一个引用另一个模型的模型,我想使用引用模型中的数据进行验证。这可能吗?

这是我的模特:

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; }
  }
}

以下是区域模型:

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; }
  }
}

我在地址模型中尝试做的事情,概念上是:

[AssertThat("ZipCodeID == District.ZIP")]
public int DistrictID { get; set; }

这似乎是合理的,因为在地址模型中引用了区模型,但它产生了这个异常:

描述:执行当前Web请求期间发生了未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

 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.

我是否遗漏了一些明显的方法来制作地区模型中的Zone.Zip PUBLIC? 我对此非常陌生,所以非常感谢任何指导或帮助!

0 个答案:

没有答案