开发者

Dynamic data validation in ASP.NET MVC

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.


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"
        }
    });
});

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜