Validation not working on plain objects in ASP.NET MVC -
When I post a controller to the 'model' object (as is generated by LinqToSQL), then I 'ModelState IsValid ', and if any property has validation properties and the value is not valid, then it will be set to' false '.
However, ModelState.IsValid is always coming back to 'true' if I am posting a custom object of my own class, even if the properties of that class have validation properties and they are incorrect values has given.
Why does this only work with data contact model objects? What about these items that they work with modelstate? Isleveld, while not the normal class?
How can I work with normal classes?
Controller code: modal code:
using the system; Using System.Collections.Generic; Using System.Linq; Using System.Web; Using System.ComponentModel.DataAnnotations; Using MyProject.Website.Validators; Using System.ComponentModel; Public Class Login {Private Login Modes Log Inmode {Receive; Set; } [Required] [email address] Receive public string email address { Set; } Public string password {get; Set; } Received Public Child Memem; Set; } Public String Returns Url {get; Set; }}
After
you have set DataAnnotationsModelBinder
your default model Such as your Global.asax
file like
protected zero application_Start () {ModelBinders.Binders.DefaultBinder = new DataAnnationModelBinder (); }
As far as I know, the features under system.ComponentModel.DataAnnation
only namescape that work with that model binder.
You can also set your model binder only for that action:
[AcceptVerbs (HttpVerbs.Post)] Public ActionResult Login ([ModelBinder (Typeof (DataAnnotationsModelBinder)]] Yieldbroker.Website.ViewModels.Shared Login Model) and see {// ...}
Comments
Post a Comment