Click here to Skip to main content
14,242,230 members
Rate this:
Please Sign up or sign in to vote.
See more:
i am submiting values on submit but ModelState.IsValidis is my code
       public ActionResult StudentInfo(StudentForValidation studentval, IEnumerable<HttpPostedFileBase> Photo)
           //Redirect to partial view
           // RedirectToAction("~/StudentInfo/Index");

           //write this code on partial view submit
               if (Photo != null)
                   foreach (var img in Photo)
                       var fileName = Path.GetFileName(img.FileName);
                       var Filepath = Path.Combine(Server.MapPath("/Images/Student"), fileName);

                       // string photoUrl = @"../../Images/" + fileName;


               if (ModelState.IsValid)

                   student s = new student();
                   if (studentval.Birthdate >= DateTime.Now)
                       ModelState.AddModelError("","Birthdate Is Wrong");
                       return View();
                   if (studentval.Middle_Name != studentval.Parent_First_Name)
                       ModelState.AddModelError("","Your First Name Mismatch with Student's Middle Name");

                   s.First_Name = studentval.First_Name;
                   s.Middle_Name = studentval.Middle_Name;
                   s.Last_Name = studentval.Last_Name;
                   s.Address_Line1 = studentval.Address_Line1;
                   s.Address_Line2 = studentval.Address_Line2;
                   s.Address_Line3 = studentval.Address_Line3;
                   s.Birthdate = studentval.Birthdate;
                   s.City = studentval.City;
                   s.Country = studentval.Country;
                   s.Distance_From_Home = studentval.Distance_From_Home;
                   s.Division = studentval.Division;
                   s.Gender = studentval.Division;
                   s.Hobbies = studentval.Hobbies;
                   s.Languages_Known = studentval.Languages_Known;

                   parent p = new parent();
                   login l = new login();



                   //create format for username
                   l.User_Name = s.First_Name + "@1234";
                   l.Password = s.First_Name + "@" + s.Birthdate;
           catch (Exception e)
           { }

           var error = smEntity.GetValidationErrors();
           int count = error.Count();

           return View();

Rate this:
Please Sign up or sign in to vote.

Solution 1

You can Get Validation Error Messages from ModelState with using this function

public static List<string> GetErrorListFromModelState
                                              (ModelStateDictionary modelState)
      var query = from state in modelState.Values
                  from error in state.Errors
                  select error.ErrorMessage;

      var errorList = query.ToList();
      return errorList;
Rate this:
Please Sign up or sign in to vote.

Solution 2

Put a debug point on here 'if (ModelState.IsValid)' and after that check the all properties value inside the ModelState object.When you do that,you'll can see which property or properties having 'false' value.Then check those properties with your view validations.

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

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100