Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
i am submiting values on submit but ModelState.IsValidis false.....here is my code
//code
C#
[HttpPost]
       public ActionResult StudentInfo(StudentForValidation studentval, IEnumerable<HttpPostedFileBase> Photo)
       {
           //Redirect to partial view
           // RedirectToAction("~/StudentInfo/Index");

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

                       // 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.State=studentval.State;
                   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();

                   smEntity.students.Add(s);
                   smEntity.SaveChanges();

                   smEntity.parents.Add(p);
                   smEntity.SaveChanges();

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


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

           RedirectToAction("Index","Index");
           return View();

       }
Posted

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

C#
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;
}
 
Share this answer
 
v4
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.
 
Share this answer
 

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