asp.net mvc - Custom IModelBinder and Database Access -
For user objects in my asp.net mvc project, I have written a custom modelbender to check that the passwords are valid Or not two matched passwords etc. were recorded.
The login name should be exclusive, however, I was wondering if I can check it in the ModelBandder, or is it considered bad behavior?
The thing is that before you even get the controller, before it is called Booker, then I will have two examples of my data floating around and so many connections from the database , I think I have a factory for that type.
What it does now in a controller, it has a code snippet:
// Post: / user / create [AcceptVerbs (HttpVerbs.Post)] Public Functionality Create (user user) {myDataContext db = new myDataContext (); If (! ViewData.ModelState.IsValid) {return view (user); } User testUser = db.Users.SingleOrDefault (p = & gt; p.LoginNaam == user.LoginNaam); If (testUser! = Null) {// here error stuff}}
model me Do not check the user's availability in the binder; I think this is the job of the CreateUser method to do this.
Then the action will be like this:
// POST: / user / create [AcceptVerbs (HttpVerbs.Post)] Create public action roles (user users) {myDataContext db = New myDataContext (); If (! ViewData.ModelState.IsValid) {return view (user); } Try {Db.CreateUser (user); } Hold (Logic Expression E) {ModelState.AddModelError (e.ParamName, e.Message); View return (user); } View return ("User-bought", user)}
Comments
Post a Comment