I have an application who use ASP.NET MVC and KnockoutJS for the MVVM part. The server bind the data to the server by JSON. The client treat data and make a standard POST callback after that.
我有一个使用ASP的应用程序。MVVM部分的MVC和KnockoutJS。服务器通过JSON将数据绑定到服务器。客户端处理数据并在此之后创建一个标准的POST回调。
I have now to handle some business logic like :
我现在必须处理一些业务逻辑,比如:
- The property1 must be superior to 10 if property2 == "FOOBAR"
- 如果property2 = "FOOBAR",则property1必须优于10
- The property2 must be != ""
- 属性2必须是!= ""
Etc etc... (there is a lot of conditions I have to handle)
等等等等……(我有很多条件要处理)
So, to make things simpler, is it possible to avoid code duplicate between the server side code (C#) and the client side (JS) ? The idea would be to define the validation rules at one place (using Data Annotations maybe) and use these rules to generate the JS code (for client side validation) or the C# code (for the server side validation).
因此,为了使事情更简单,是否可能避免服务器端代码(c#)和客户端代码(JS)之间的代码重复?其思想是在一个地方定义验证规则(可能使用数据注释),并使用这些规则生成JS代码(用于客户端验证)或c#代码(用于服务器端验证)。
Thanks by advance !
提前谢谢!
1 个解决方案
#1
1
I would imagine that the built in annotations combined with adding jQuery validation would suit most of your business requirements.
我可以想象,内置的注释与添加jQuery验证相结合,将适合您的大多数业务需求。
For example, your second one can be achieved using [Required]
例如,可以使用[Required]实现第二个
For the ones that require something a little extra (like the first scenario you mentioned), you could either take a look at Remote
validation or implement your own annotations (but that gets a little trickier when hooking into jQuery validation to add your new annotations).
对于那些需要额外的东西(比如您提到的第一个场景),您可以查看远程验证,或者实现您自己的注解(但是当连接到jQuery验证以添加新的注解时,这就有点麻烦了)。
#1
1
I would imagine that the built in annotations combined with adding jQuery validation would suit most of your business requirements.
我可以想象,内置的注释与添加jQuery验证相结合,将适合您的大多数业务需求。
For example, your second one can be achieved using [Required]
例如,可以使用[Required]实现第二个
For the ones that require something a little extra (like the first scenario you mentioned), you could either take a look at Remote
validation or implement your own annotations (but that gets a little trickier when hooking into jQuery validation to add your new annotations).
对于那些需要额外的东西(比如您提到的第一个场景),您可以查看远程验证,或者实现您自己的注解(但是当连接到jQuery验证以添加新的注解时,这就有点麻烦了)。