排除得到验证的一些子成员 [英] Exclude some child members from getting validated

查看:56
本文介绍了排除得到验证的一些子成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要注册新会员我有一个名为视图模型 UserRegisterModel 。这种模式得到两种地址来自用户,他的是homeAddress 这就是要求和他的招聘人数这就是可选即可。为了得到我的地址使用命名的复杂类型 ContactEntryModel 。我饰我ContactEntryModel成员造成家庭和工作地址得到自动验证 [必需] 属性。

我在寻找一个解决方案,以纪念招聘人数为忽略排除并通过告诉validatation发动机停止验证招聘人数子属性,即使它们装饰着验证属性。

 公共类UserRegisterModel
    {
        [StringLength(50),必需]
        公共字符串名字{获得;组; }
        [StringLength(50),必需]
        公共字符串名字{获得;组; }
        [StringLength(10),必需]
        公共字符串IdCardNo {搞定;组; }
        [StringLength(100),发送电子邮件]
        公共字符串电子邮件{获得;组; }        公共ContactEntryModel首页联系{搞定;组; } //必填        公共ContactEntryModel WorkContact {搞定;组; } //可选的
     }
   公共类ContactEntryModel
    {
        [MAXLENGTH(4),必需]
        公共字符串电话$ P $ {PFIX获得;组; }
        [MAXLENGTH(10),必需]
        公共字符串电话{搞定;组; }
        [MAXLENGTH(50),必需]
        公共字符串省{搞定;组; }
        [MAXLENGTH(50),必需]
        公共字符串城{搞定;组; }
        [MAXLENGTH(300),必需]
        公共字符串AddressLine {搞定;组; }
        [MAXLENGTH(20)]
        公共字符串邮政code {搞定;组; }
    }


解决方案

要做到这一点,最好的方法是创建一个适当的装饰,以你的情况自定义视图模式。

另一个版本是要告诉你的模型绑定忽略的字段(这也跳过验证)。您可以使用绑定属性做到这一点:

  [绑定(不包括=招聘人数)]
公众的ActionResult DoSomething的(UserRegisterModel模型)
{
    //控制器code在这里
}

To register new members I have a ViewModel named UserRegisterModel. this model gets two kind of address from user, his HomeAddress which is required and his WorkAddress which is optional. To get addresses I use a complex type named ContactEntryModel. I decorated my ContactEntryModel members with [Required] attribute which cause both home and work address get validated automatically.

I'm searching for a solution to mark WorkAddress as Ignored or Excluded and by that telling the validatation engine to stop validating WorkAddress child properties even though they decorated with validation attributes.

public class UserRegisterModel
    {
        [StringLength(50), Required]
        public string FirstName { get; set; }
        [StringLength(50), Required]
        public string LastName { get; set; }
        [StringLength(10), Required]
        public string IdCardNo { get; set; }
        [StringLength(100), Email]
        public string Email { get; set; }

        public ContactEntryModel HomeContact { get; set; }  //Required

        public ContactEntryModel WorkContact { get; set; }  //Optional
     }


   public class ContactEntryModel
    {
        [MaxLength(4), Required]
        public string TelPrefix { get; set; }
        [MaxLength(10), Required]
        public string Tel { get; set; }
        [MaxLength(50), Required]
        public string Province { get; set; }
        [MaxLength(50), Required]
        public string City { get; set; }
        [MaxLength(300), Required]
        public string AddressLine { get; set; }
        [MaxLength(20)]
        public string PostalCode { get; set; }
    }

解决方案

The best way to do this is to create a custom view model that is decorated appropriately to your situation.

Another version would be to tell your model binder to ignore the fields (this also skips the validation). You can do this using the Bind attribute:

[Bind(Exclude="WorkAddress")]
public ActionResult DoSomething(UserRegisterModel model)
{
    //controller code here
}

这篇关于排除得到验证的一些子成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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