禁用某些字段的验证 [英] Disable validation on certain fields

查看:125
本文介绍了禁用某些字段的验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个视图模型添加具有属性的用户:电子邮件,密码,ConfirmPassword对所有属性必需属性。当编辑用户想不被需要的密码和ConfirmPassword属性。

I've got a ViewModel for adding a user with properties: Email, Password, ConfirmPassword with Required attribute on all properties. When editing a user I want the Password and ConfirmPassword properties not to be required.

有没有一种方法来禁用在不同的控制器操作的某些属性验证,或只是最好创建一个单独的EditViewModel?

Is there a way to disable validation for certain properties in different controller actions, or is it just best to create a seperate EditViewModel?

推荐答案

我想打破它,并与所有常用数据的基础模型和inhierit为每个视图:

I like to break it down and make a base model with all the common data and inhierit for each view:

class UserBaseModel
{
    int ID { get; set; }

    [Required]
    string Name { get; set; }       

    [Required]
    string Email { get; set; }               
    // etc...
}

class UserNewModel : UserBaseModel
{
    [Required]
    string Password { get; set; }

    [Required]
    string ConfirmPassword { get; set; }
}

class UserEditModel : UserBaseModel
{
    string Password { get; set; }
    string ConfirmPassword { get; set; }
}

有兴趣知道是否有更好的方法,以及虽然这种方式似乎很干净的灵活性。

Interested to know if there is a better way as well although this way seems very clean an flexible.

这篇关于禁用某些字段的验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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