一个或多个实体的验证失败. [英] Validation failed for one or more entities.

查看:41
本文介绍了一个或多个实体的验证失败.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试更新IsApproved = true批准该属性时,出现以下错误. 一个或多个实体的验证失败.有关更多详细信息,请参见'EntityValidationErrors'属性.验证错误是:请上传图像.请选择一些属性. 请帮帮我.提前感谢. 我的模型如下:

When I try to Update IsApproved=true to approve the property, I am getting the following error. Validation failed for one or more entities. See 'EntityValidationErrors' property for more details. The validation errors are: Please upload the image.; Please select some property features. Please help me out. Thanx in advance. My Model is as follows:

[Table("AddProperty")] 公共类AddProperty { [Key,DatabaseGenerated(DatabaseGeneratedOption.Identity)] [HiddenInput(DisplayValue = false)] public int ID {get;放; }

[Table("AddProperty")] public class AddProperty { [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] [HiddenInput(DisplayValue = false)] public int Id { get; set; }

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

推荐答案

使用此跟踪错误:

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

这篇关于一个或多个实体的验证失败.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆