一个或多个实体的验证失败。

时间:2014-04-19 19:56:47

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

当我尝试更新IsApproved = true以批准该属性时,我收到以下错误。 一个或多个实体的验证失败。请参阅' EntityValidationErrors'物业详情。验证错误包括:请上传图片。请选择一些酒店功能。 请帮帮我。 Thanx提前。 我的模型如下:

[表("方法addProperty&#34)]     公共类AddProperty     {         [Key,DatabaseGenerated(DatabaseGeneratedOption.Identity)]         [HiddenInput(DisplayValue = false)]         public int Id {get;组; }

    public List<TransactionType> TransactionType_List { get; set; }//Used to populate dropdown list values

    [Required(ErrorMessage = "Please select the category.")]
    [Display(Name = "Category:")]
    public int TransactionTypeId { get; set; }//Used to post back selected value

    public virtual TransactionType TransactionType { get; set; }

    public List<PropertyType> PropertyType_List { get; set; }//Used to populate dropdown list values

    [Required(ErrorMessage = "Please select the property.")]
    [Range(1, int.MaxValue, ErrorMessage = "Please select the property.")]
    [Display(Name = "Property:")]
    public int PropertyTypeId { get; set; }//Used to post back selected value

    public virtual PropertyType PropertyType { get; set; }

    public List<PropertyList> PropertyList_List { get; set; }//Used to populate dropdown list values

    [Required(ErrorMessage = "Please select the property type.")]
    [Range(1, int.MaxValue, ErrorMessage = "Please select the property type.")]
    [Display(Name = "Property Type:")]
    public int PropertyListId { get; set; }//Used to post back selected value

    public virtual PropertyList PropertyList { get; set; }

    [Required(ErrorMessage = "Property Name is required.")]
    [StringLength(50, MinimumLength = 3, ErrorMessage = "Building Name length should be between 3 and 50.")]
    [Display(Name = "Property Name:")]
    public string PropertyName { get; set; }

    public List<FlatDescription> FlatDescription_List { get; set; }//Used to populate dropdown list values

    [Required(ErrorMessage = "Description is required.")]
    [Display(Name = "Description:")]
    public int FlatDescriptionId { get; set; }

    public virtual FlatDescription FlatDescription { get; set; }

    public List<Bathroom> Bathrooms_List { get; set; }//Used to populate dropdown list values

    [Required(ErrorMessage = "No of Bathrooms is required.")]
    [Display(Name = "No of Bathrooms:")]
    public int BathroomId { get; set; }//Used to post back selected value

    public virtual Bathroom Bathroom { get; set; }

    public List<City> City_List { get; set; }//Used to populate dropdown list values

    [Required(ErrorMessage = "Please select the city.")]
    [Display(Name = "City:")]
    public int CityId { get; set; }//Used to post back selected value

    public virtual City City { get; set; }

    [Required(ErrorMessage = "Location is required.")]
    [StringLength(30, MinimumLength = 3, ErrorMessage = "Location length should be between 3 and 30.")]
    [Display(Name = "Location:")]
    public string Location { get; set; }

    [Required(ErrorMessage = "Property Price is required.")]
    [Range(typeof(decimal),"1","10000000",ErrorMessage = "Please enter valid property price.")]
    [RegularExpression(@"^\d+.\d{0,2}$", ErrorMessage = "Only 2 decimal point values are allowed.")]
    [Display(Name = "Enter Property Price:")]
    public decimal PropertyPrice { get; set; }

    //[Required(ErrorMessage = "Please upload the image.")]
    [Display(Name = "Upload Image:")]
    [NotMapped]
    [ValidatePhoto]
    public HttpPostedFileBase PropertyPhoto { get; set; }

    public string ImageURL { get; set; }

    public List<Facilities> Facilities_List { get; set; }//Used to populate dropdown list values

    [Required(ErrorMessage = "Please select some property features.")]
    [Display(Name = "Select Property Features:")]
    [NotMapped]
    public int[] SelectedIds { get; set; }//Used to post back selected value   

    public string PropertyFeatures { get; set; }

    [HiddenInput(DisplayValue = false)]
    public DateTime CreateDate { get; set; }

    [HiddenInput(DisplayValue = false)]
    public DateTime? UpdateDate { get; set; }

    [HiddenInput(DisplayValue = false)]
    public int CreateUser { get; set; }

    [HiddenInput(DisplayValue=false)]
    public int? UpdateUser { get; set; }

    [HiddenInput(DisplayValue = false)]
    public bool IsApproved { get; set; }

    [HiddenInput(DisplayValue = false)]
    public bool IsActive { get; set; }             
}

1 个答案:

答案 0 :(得分:1)

使用它来跟踪错误:

catch (DbEntityValidationException e)
{
    foreach (var eve in e.EntityValidationErrors)
    {
        Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
            eve.Entry.Entity.GetType().Name, eve.Entry.State);
        foreach (var ve in eve.ValidationErrors)
        {
            Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                ve.PropertyName, ve.ErrorMessage);
        }
    }
    throw;
}