Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 验证属性未返回任何错误_C#_Asp.net_Webforms - Fatal编程技术网

C# 验证属性未返回任何错误

C# 验证属性未返回任何错误,c#,asp.net,webforms,C#,Asp.net,Webforms,我有一个实体,我为其添加了符号,以验证输入。但是,它不起作用。我浏览的所有信息都是针对MVC的,但我的项目不是MVC项目。有没有办法为非MVC项目启用客户端数据注释?或者是我做错了什么或错过了什么?如果它只针对MVC,那么对于非MVC web应用程序项目是否有类似的替代方案?我总是可以自己验证,但那一个似乎更好,而且是客户端验证 这是我的实体类: public class Customer { [Key] public int CustomerId

我有一个实体,我为其添加了符号,以验证输入。但是,它不起作用。我浏览的所有信息都是针对MVC的,但我的项目不是MVC项目。有没有办法为非MVC项目启用客户端数据注释?或者是我做错了什么或错过了什么?如果它只针对MVC,那么对于非MVC web应用程序项目是否有类似的替代方案?我总是可以自己验证,但那一个似乎更好,而且是客户端验证

这是我的实体类:

  public class Customer
    {

        [Key]
        public int CustomerId { get; set; }

        public virtual ICollection<Order> Orders { get; set; }

        public int AddressId { get; set; }

        [ForeignKey("AddressId")]
        public virtual Address BillingAddress  { get; set; }

        [Required(ErrorMessage = "Customer Name is required")]
        [DisplayName("Customer Name")]
        [StringLength(150)]
        public string FirstName { get; set; }

        [Required(ErrorMessage = "Customer Middle Name is required")]
        [DisplayName("Middle")]
        [StringLength(150)]
        public string MiddleName { get; set; }

        [Required(ErrorMessage = "Customer Last Name is required")]
        [DisplayName("Last")]
        [StringLength(150)]
        public string LastName { get; set; }

        [Required(ErrorMessage = "Email Address is required")]
        [DisplayName("Email Address")]
         [RegularExpression(@"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}",
        ErrorMessage = "Email is is not valid.")]
        public string Email { get; set; }

        [Required(ErrorMessage = "Telephone is required")]
        [DisplayName("Telephone")]
        public string Phone { get; set; }


    }
谢谢大家!

  protected void btnSubmit_Click(object sender, EventArgs e)
        {

            //extractCartItems();

            using (var context = new EntityMappingContext())
            {

                //initialize type to avoid nulle refference

                //CreateAddress
                Address billingAddress = new Address()
                {

                    //Address part
                    StreetName = txtStreetName.Text,
                    StreetNo = txtStreetNo.Text,
                    City = txtCity.Text,
                    Country = txtCountry.Text,
                    ZipCode = txtZipCode.Text,

                };

                //Create customer
                Customer customer = new Customer()
                {

                    FirstName = txtFirstName.Text,
                    MiddleName = txtMiddleName.Text,
                    LastName = txtLastName.Text,
                    Phone = txtTelephone.Text,
                    Email = txtEmail.Text,

                    BillingAddress = billingAddress

                };


                try
                {
                    ctx.Customers.Add(customer);
                    ctx.SaveChanges();

                    m_lastOrderId = order.OrderId;

                    Session["lastId"] = m_lastOrderId;


                    //Activate validation 
                    ctx.Configuration.ValidateOnSaveEnabled = true;

                }
                catch (DbEntityValidationException ex)
                {

                    //Session["Error"] = ex;
                    //Response.Redirect("ErrorPage.aspx");

                    var errorMessages = ex.EntityValidationErrors.SelectMany(x => x.ValidationErrors).Select(x => x.ErrorMessage);

                    //join the list to a stingle string
                    var fullErrorMessage = string.Join("; ", errorMessages);

                    //combine the original exception message with the new one

                    var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage);

                    Session["Error"] = fullErrorMessage;

                    Response.Redirect("ErrorPage.aspx");

                    //Throw a new DBEntityValidationException with the imrpoved exception message
                    throw new DbEntityValidationException(exceptionMessage, ex.EntityValidationErrors);

                }