验证属性不返回任何错误

时间:2014-03-17 08:53:35

标签: c# asp.net webforms

我有一个实体,我已为其添加了符号以验证输入。但是,它不起作用。我浏览的所有信息都是针对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);

                }

谢谢!

0 个答案:

没有答案