Click here to Skip to main content
15,891,864 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
[HttpPost]
public ActionResult Create(Users user) {
    if (ModelState.IsValid) {
        db.Users.Add(user);
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    return View(user);
}


ModelState.IsValid is always false.
so it just return view and new record is not getting added.

What I have tried:

C#
[HttpPost]
public ActionResult Create(Users user) {
    if (ModelState.IsValid) {
        db.Users.Add(user);
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    return View(user);
}


ModelState.IsValid is always false.
so it just return view and new record is not getting added.
Posted
Updated 3-Feb-17 4:06am
v2
Comments
Afzaal Ahmad Zeeshan 3-Feb-17 7:42am    
So?

You probably have submitted an invalid POST that is detected by some validation checks which you did not included in your question. The view should provide an error message in such cases.

A good description can be found at What is the ModelState? - ASP.NET MVC Demystified[^]-
 
Share this answer
 
It tough to help you find out what going on with a few line of codes. Basically, during the post, one or more of the model property is failing the validation. For instance, phone number, you might have had specified required validation attributes but never provide the information during the post.

Here is a link can give you some idea on how to debug the error.

What is the ModelState? - ASP.NET MVC Demystified[^]
 
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