Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is my class,

C#
[Bind(Exclude = "Id,Language")]
   public class Register
   {
       [Key]
       public int Id { get; set; }
       [Required(ErrorMessage = "UserName is Required")]
       [StringLength(15, MinimumLength = 5, ErrorMessage = "UserName length between 5 and 15")]
       public string UserName { get; set; }
   }
    [Required(ErrorMessage = "Full Name is required")]
       public string FullName { get; set; }
    [Required(ErrorMessage = "Password is requited")]
       public string Password { get; set; }

       [NotMapped]
       [Required(ErrorMessage = "Confirm password is required")]
       [Compare("Password", ErrorMessage = "Password mismatch")]
       public string ConfirmPassword { get; set; }
      [Required(ErrorMessage = "Email is required")]
       [DataType(DataType.EmailAddress)]
       public string Email { get; set; }

       [Required(ErrorMessage = "Phone No is Required")]
       [StringLength(10, MinimumLength = 10, ErrorMessage = "Invalid Phone Number")]
       public string PhoneNo { get; set; }
       [Required(ErrorMessage = "Please select Gender")]
       public string Gender { get; set; }


       public string Address { get; set; }

       [Required(ErrorMessage = "Please Select Language")]
       public string SelectedLanguage{get;set;}
       public List<string> Language
       {
           get
           {
               List<string> language = new List<string>();
               language.Add("Tamil");
               language.Add("English");
               language.Add("Malayalam");
               language.Add("Hindi");
               return language;
           }
           set
           {
               // value = language;
           }
       }
        [Required(ErrorMessage = "Please Select Country")]
       public string SelectedCountry { get; set; }

       [Required(ErrorMessage = "Please accept terms and conditions")]
       public bool Accept { get; set; }

     }

this is my Controlller
C#
public class HomeController : Controller
   {
       RegContext con = new RegContext();
       [HttpGet]
       public ActionResult Register()
       {
           return View(new Register());
       }
       [HttpPost]
       public ActionResult Register(Register register, FormCollection collection)
       {
           string val = "";
           foreach (var x in register.Language)
           {
               if (collection[x].ToString().Contains("true"))
               {
                   val = val + x + ",";
               }
           }
           register.SelectedLanguage = val;
          try
           {
               TryUpdateModel(register);
               con.register.Add(register);
               con.SaveChanges();
               return RedirectToAction("Success");
           }
          catch (DbEntityValidationException ex)
          {
              StringBuilder sb = new StringBuilder();

              foreach (var failure in ex.EntityValidationErrors)
              {
                  sb.AppendFormat("{0} failed validation\n", failure.Entry.Entity.GetType());
                  foreach (var error in failure.ValidationErrors)
                  {
                      sb.AppendFormat("- {0} : {1}", error.PropertyName, error.ErrorMessage);
                      sb.AppendLine();
                  }
              }

              throw new DbEntityValidationException(
                  "Entity Validation Failed - errors follow:\n" +
                  sb.ToString(), ex
              ); // Add the original exception as the innerException
          }
          return View();
       }
       public ActionResult Success()
       {
           return View();
       }
   }

I am getting error "Validation failed for one or more entities. See 'EntityValidationErrors' property for more details."
but i want to dispalay "User Name is required" if the UserName is blank, but when i run instead of displaying on the client side it goes to debugging.How can i achive this,
Posted
Comments
[no name] 8-Oct-14 6:57am    
In your post method you have not used if(ModelState.IsValid()) first check this.Please post the post form view page also. Check there ifyou have used @Html.ValidationMessageFor(m=>m.UserName)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900