如何使用ajax在MVC 5中处理服务器端验证。 [英] how to handle server side validation in MVC 5 using ajax.

查看:57
本文介绍了如何使用ajax在MVC 5中处理服务器端验证。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有登录表单,我通过Ajax调用验证用户登录凭据。当用户输入无效密码时,我将从我的控制器验证方法返回返回视图()。在这种情况下,我想显示验证摘要消息。



我怎么能如果用户在MVC 5中使用Ajax调用输入无效密码,则显示验证摘要消息?



注意:



我正在使用JQuery ajax调用。

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.

推荐答案

Hello Amit,

MVC Razor提供了验证摘要。

您只需要在View PAge或Login PAge中使用@ Html.ValidationSummary(true)。然后它将显示验证错误。如果已存在Dataannotation属性,即已添加。



请查看此内容。

我希望这可以帮助您。

谢谢
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


对此最好的解决方案是使用客户端验证(如jQuery,JavaScript)来检查密码及其长度。如果有效则将其发送到服务器。



但是由于你需要使用ajax,我建议你不要使用Views返回。而是为 AjaxRequests 创建一个单独的Controller,并在其中创建不同的Actions。他们将处理请求,并返回简单的字符串结果而不是整个视图。



这样可以最大限度地减少对数据大小的要求,并且很简单,因为一旦你得到结果,你只需将该字符串附加到你的DOM中(使用一个< p>< / p> 元素。。
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).


我已经解决了我的问题,我使用了ViewBag .Error =密码错误。并返回视图。它做我想要的。'



感谢所有其他人的帮助。
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.


这篇关于如何使用ajax在MVC 5中处理服务器端验证。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆