I am using Visual studio 2012 and Entity Framework 6.I downloaded update for MVC5. I created a model class CreateUser
as below
public class CreateNewUser
{
[Required]
[Display(Name = "Name")]
public String Name{set;get;}
[Required]
[Display(Name = "Mobile")]
[RegularExpression(@"^([0|\+[0-9]{1,5}[\-\s])?([7-9][0-9]{9})$", ErrorMessage = "Entered phone format is not valid.")]
public String Mobile { get; set; }
[Required]
[EmailAddress]
[Display(Name = "Email")]
public String Email { get; set; }
[Required]
[Display(Name = "Password")]
[DataType(DataType.Password)]
public String Password { get; set; }
}
and create a view using this model class. when i run the form it is submitted with out validating . i used ModelState.IsValid
at server and step over it. it always shows true even if the form has empty fields. The client side and server side validation worked in mvc3
and mvc4
. But i moved to mvc5
it is not working. ClientValidationEnabled
, UnobtrusiveJavaScriptEnabled
are set to true in webconfig
. Is there anything in that I need to set in MVC5
for enabling validation
Having a strongly typed View
submit and trigger client side validation is usually something that works out of the box.
For example, I just created a new project in VS 2013 (not VS 2012) and chose the MVC template with Individual User Accounts
(to have all the appropriate scripts loaded, bundled, etc.).
Then, I created a ViewModel called CreateNewUser
and copy/pasted all your properties.
Then, I create two ActionResult in my Controller. One to load the View and the other to handle the submit of the form.
public ActionResult CreateUser()
{
var model = new CreateNewUser();
return View(model);
}
[HttpPost]
public ActionResult CreateUser(CreateNewUser model)
{
if (!ModelState.IsValid)
{
return View(model);
}
//Model is valid so continue...
return null;
}
Then, I created the View which is strongly typed to this CreateNewUser
class.
When I click the submit button, the form's client side validation works perfectly!
Perhaps, you should create a brand new project regardless of VS2012 or VS2013 and create a simple example like I did. From there, make it work and when it does, try to compare the things that are different with your current situation.
That's how I would try and find a solution to my problem by comparing a solution that works with one that doesn't in order to pin point the real culprit.
Hope this helps!
If you are evaluating the IsValid flag through unit tests it seems that it is always true and validation attributes are ignored completely (yet works fine when the same code it executed through IIS Express).
We found a way to overcome this was to invoke controller.TryValidateModel before checking IsValid. If you don't want to pollute your production code you can do this using reflection from the unit test.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.