c# - Displaying validation messages in the view (jQuery Validation) -


the goal

show custom errors on view using jquery validation.

the problem

everything working well. yes... everything! need light problem. i've searched on google , stack until now, no success.

okay, lets go: log in form composed email , password validating — throws message if email invalid or if password field empty. need is: throw message if email + password combination doesn't exist on database, or, in other words, if user doesn't exist.

take in following fragment:

[...]  [acceptverbs(httpverbs.post)] [validateantiforgerytoken] [allowanonymous] public actionresult authenticate(user usermodel) {     if (modelstate.isvalid)     {         if (usermodel.isvalid(usermodel.email, usermodel.password))         {             formsauthentication.setauthcookie(usermodel.email, usermodel.remember);             return redirecttoaction("index");         }         else         {             modelstate.addmodelerror(string.empty, "login data incorrect!");         }     }     return view(usermodel); }  [...] 

pay attention @ line:

modelstate.addmodelerror(string.empty, "login data incorrect!"); 

i want display in view "login data incorrect!" not know how can this.

code spotlight

i'm using jquery validation unobtrusive , that's — nothing more. called script , application goes on.

by way, if necessary, follow markup:

[...] @using (ajax.beginform("authenticate", "manager",             new ajaxoptions{ httpmethod = "post" })){     <ul>         <li>             @html.labelfor(m => m.email, "what's email?")             @html.textboxfor(m => m.email, "", new { placeholder = "email" })             @html.validationmessagefor(m => m.email)         </li>         <li>             @html.labelfor(m => m.password, "what's password?")             @html.textboxfor(m => m.password, "", new { placeholder = "password" })             @html.validationmessagefor(m => m.password)         </li>         <li>             @html.checkboxfor(m => m.remember)             remember         </li>         <li>             <button type="submit" class="btn btn-primary">log in</button>         </li>     </ul> } [...] 

to remember

i call jquery validation's scripts , (simple) validation marked annotation @ top of properties on model works magic.

to improve question comprehension, follow sneak peak through user's model:

[...]  [required] [emailaddress] [datatype(datatype.emailaddress)] [display(name = "email")] public string email { get; set; }  [required] [datatype(datatype.password)] [display(name = "password")] public string password { get; set; }  [...] 

assuming have method checks user , returns bool (true|false), based on return value use following.

 if (!_userservice.getuser("username"))   {     modelstate.addmodelerror("username", "account not found");     return view();   } 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -