ASP.NET MVC3和服务器端验证服务器端、ASP、NET

2023-09-03 20:31:31 作者:心癌晚期苦瓜村长。

假设用户已禁用JavaScript,因此客户端验证不能在MVC3工作。

Suppose a user has javascript disabled and thus client side validation doesn't work in MVC3.

什么是实现服务器端验证,所以当用户试图处理数据的方式不好?该验证消息仍然显示的最佳方式

What is the best way to implement server side validation so that validation messages are still displayed when the user tries to handle data in an inappropriate way?

谢谢!

编辑:

显然,这是怎么回事,因为我使用的是EF生成的模型和他们使用的属性setterStructuralObject.SetValidValue的方法。这将导致异常MVC之前被抛出可以验证模型。

Apparently it's happening because I'm using EF generated models and they use "StructuralObject.SetValidValue" methods in property setters. This results in an exception being thrown before MVC can validate the model.

我试图找到一种方法,现在来规避这一权利...

I'm trying to find a way to circumvent this right now...

推荐答案

那么,你应该总是使用客户端和服务器端验证。如果标记的模型验证特性在服务器端和客户端验证应该工作得很好。

Well, you should always use client side and server side validation. If you mark the models with validation attributes both the server-side and client-side validation should work just fine.

我相信你已经看到了这一点:的http:// bradwilson.typepad.com/blog/2010/10/mvc3-unobtrusive-validation.html

I am sure you have seen this: http://bradwilson.typepad.com/blog/2010/10/mvc3-unobtrusive-validation.html

只要确保在服务器端验证的行动检查模型的状态一次,一切都将工作的伟大。

Just be sure to check model state once in the action for the server-side validation and everything will work great.