在ASP.NET MVC中的动态数据验证 [英] Dynamic data validation in ASP.NET MVC

查看:183
本文介绍了在ASP.NET MVC中的动态数据验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近阅读了关于ASP.NET MVC的模型验证功能,这些都非常酷,直到某一点。如果应用程序不知道它工作的数据,会发生什么,因为它都存储在DB中,并在运行时一起构建。就像在Drupal,我想能够在运行时定义自定义类型,并分配运行时验证规则。显然,将属性分配给完善的模型的想法现在已经不复存在了。还能做什么?我想在规则作为JSON对象存储在DB字段或类似的东西。

I've recently read about the model validation capabilities of ASP.NET MVC which are all very cool until a certain point. What happens if the application doesn't know the data that it works with because it is all stored in DB and built together at runtime. Just like in Drupal, I'd like to be able to define custom types at runtime, and assign runtime validation rules as well. Obviously, the idea of assigning attributes to well established models is now gone. What else could be done ? I am thinking in terms of rules being stored as JSON objects in the DB fields or something like that.

推荐答案

jquery验证插件?你有一个选项是在Javascript中声明你的UI验证。例如,对于我的联系页,我使用了以下验证。

Have you looked at the jquery validation plugin? One of the options you have there is to declare your UI validation in Javascript. For example for my contact page I have the following validation being used.

$(document).ready(function () {
    $("#ContactForm").validate({
        rules: {
            Name: "required",
            Email: {
                required: true,
                email: true
            },
            Subject: "required",
            Message: "required"
        }
    });
});

这是一个非常baisc的插件使用。

This is a very baisc usage of the plugin.

显然,你仍然需要一些后端验证,但对于你的UI,这对你的场景来说是理想的。

Obviously you will still need some sort of backend validation, but for you UI this sounds ideal to your scenario.

这篇关于在ASP.NET MVC中的动态数据验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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