ASP.NET MVC中的动态数据验证

时间:2022-01-23 15:54:31

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.

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

1 个解决方案

#1


1  

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.

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

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

This is a very baisc usage of the plugin.

这是插件的非常有用的用法。

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

显然,您仍然需要某种后端验证,但对于您来说,这听起来非常适合您的场景。

#1


1  

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.

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

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

This is a very baisc usage of the plugin.

这是插件的非常有用的用法。

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

显然,您仍然需要某种后端验证,但对于您来说,这听起来非常适合您的场景。