asp.net mvc - Exclude some child members from getting validated -
to register new members have viewmodel named userregistermodel. model gets 2 kind of address user, homeaddress required , workaddress optional. addresses use complex type named contactentrymodel. decorated contactentrymodel members [required] attribute cause both home , work address validated automatically.
i'm searching solution mark workaddress ignored or excluded , telling validatation engine stop validating workaddress child properties though decorated 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 create custom view model decorated appropriately situation.
another version tell model binder ignore fields (this skips validation). can using bind attribute:
[bind(exclude="workaddress")] public actionresult dosomething(userregistermodel model) { //controller code here }
Comments
Post a Comment