ASP.NET MVC - 用于客户端验证的数据注释

发布于 2024-08-08 20:03:53 字数 262 浏览 11 评论 0原文

如果 ASP.NET MVC 应用程序使用数据注释...

<%= Html.ValidationSummary("Things broke...") %>
<% Html.EnableClientValidation(); %> 

并且我们将其发布到服务器。我们是否仍然会点击该操作,检查 ModelState.IsValid 并返回到带有验证错误的原始视图。如果我们(通过帖子)访问服务器,它是否仍被视为客户端验证?

If an ASP.NET MVC application using Data Annotations...

<%= Html.ValidationSummary("Things broke...") %>
<% Html.EnableClientValidation(); %> 

And we post to the server. Won't we still hit the action, check the ModelState.IsValid and come back to the original view with the validation error. Is it still considered client side validation if we're hitting the server (via a post)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

云柯 2024-08-15 20:03:53

不,你刚才所说的不被视为客户端验证。

客户端验证被认为是验证浏览器(客户端)中表单的任何内容,通常使用 JavaScript。将帖子发送到服务器 (ASP.net) 后,您就处于服务器端验证模式。

我相信您真正要问的是新的 asp.net mvc 2.0 验证是否是客户端。

EnableClientValidation 使您的数据注释模型能够使用 jquery 验证插件来执行真正的客户端验证。它不应该回发到服务器来进行验证,但是当它回发时,它可能也会在服务器上进行验证,因为客户端验证不是 100% 可靠。

No, what you just said is not considered client side validation.

Client side validation is considered anything that validates the form in the browser (client), usually with JavaScript. Once the post is sent to the server (ASP.net), then you are in server side validation mode.

I believe what you are really asking though is whether the new asp.net mvc 2.0 validation is client side or not.

EnableClientValidation enables your data annotated models to use the jquery validate plugin to do true client side validating. It should not be posting back to the server to do the validating, but when it does post back it will probably validate on the server as well since client side validation is not 100% reliable.

单身情人 2024-08-15 20:03:53

是的,这就是服务器端验证。如果您在客户端进行验证并避免在任何字段无效时发布表单,那么它就是客户端验证。

Yes, that would be server side validation. If you do the validation on the client side and avoid the form to be posted when any of the fields are invalid, then it's client side validation.

我纯我任性 2024-08-15 20:03:53

如果您添加这些库,它将负责客户端验证。

<script type="text/javascript" src="MicrosoftAjax.js"></script>
<script type="text/javascript" src="MicrosofMVCValidation.js"></script>

这些库将生成用于验证的 JavaScript 代码。
但为此,您需要使用 Dataannotations

这里是示例

If you add these libraries it will take care of the client validation.

<script type="text/javascript" src="MicrosoftAjax.js"></script>
<script type="text/javascript" src="MicrosofMVCValidation.js"></script>

These libraries will generate the javascript code for validation.
But for this you need to use Dataannotations

Here is an example about it

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文