Dynamic data validation in ASP.NET MVC

随声附和 提交于 2019-12-25 01:33:25

问题


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.


回答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.

$(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.



来源:https://stackoverflow.com/questions/2478175/dynamic-data-validation-in-asp-net-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!