Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all,

I have login form, I am validating the user login credentials through Ajax call. When user enter invalid password then i am returning the return view() from my controller validating method, In this case i want to show validation summary message.

How can i show the validation summary message if user enter invalid password using Ajax call in MVC 5?

Note:

I am using JQuery ajax call.
Posted

Hello Amit,
MVC Razor provides the Validation Summary to be shown.
You just need @Html.ValidationSummary(true) in the View PAge, or the Login PAge. Then it will show the validation errors. If the Dataannotation attributes are already present i.e. you have added.

Please check with this.
I hope this helps you.
Thanks
 
Share this answer
 
Comments
amit_83 11-Feb-15 8:04am    
I have used validation summary, But i am using Jquery Ajax method to validate the login, i am entering invalid password, When it come to ajax failure then, Then how can i show validation summary message from thee?
Passion4Code 11-Feb-15 8:06am    
Then you can use ModelState.AddModelError("Password","Message goes here")
This will add the message at the ValidationMessageFor(m=>m.Password) or whatever you have used. I hope you got.
amit_83 11-Feb-15 8:09am    
Thanks for reply.

i did same as ModelState.AddModelError("Password","Message goes here") as well as i am returning View("Error"). But it is not showing Validation summary error.
Method is HttpPost.
amit_83 11-Feb-15 8:12am    
I might be missing something, Can you give me a code example?
I am new for MVC5.
Passion4Code 11-Feb-15 8:25am    
http://www.codeproject.com/Articles/710702/ASP-NET-MVC-Server-Side-Validation
http://www.codeproject.com/Articles/718004/ASP-NET-MVC-Client-Side-Validation
Both are different follow this
A best solution to this is to use a client-side validation such as jQuery, JavaScript like something to check the password and the length of it. If that is valid then send it to the server.

But since you require to use ajax for this, I would like to recommend that you do not use the Views to be returned. Instead create a seperate Controller for AjaxRequests, and make different Actions inside it. They would handle the requests, and would return simple string results not an entire view.

This would minimize the requirement of the data size, and would be simple, because once you would get the result you can simply just appened that string into your DOM (using a <p></p> element).
 
Share this answer
 
Comments
amit_83 13-Feb-15 0:39am    
Thanks for help, I have used ViewBag.Error = "Password error". and return viw. It does what i want.'

Thanks for all your positive supprt.
Afzaal Ahmad Zeeshan 13-Feb-15 5:54am    
If my solution worked for you, you can mark it as answer so that other people would be able to understand that this helped you and will help them too. :)
amit_83 18-Feb-15 7:28am    
Hi Afzal,
I read your post, Its good idea to do like that because it will reduce get/post data size. I am new for MVC, Can you give me any example to achieve this?
I have solved the from my my self, I have used ViewBag.Error = "Password error". and return view. It does what i want.'

Thanks for all others help.
 
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