0

I have the following classes:

public class Person
{
   public PhoneContact PreferredContact { get; set; }
   public PhoneContact SecondaryContact { get; set; }
   public string Email                  { get; set; }
}

public class PhoneContact
{
   [Required(ErrorMessage = "Type Required")]
   public string Type { get; set; }
   [Required(ErrorMessage = "Prefix Required")]
   public string Prefix { get; set; }
   [Required(ErrorMessage = "Number Required")]
   public string Number { get; set; }
}

And I have the following view:

<h2>Preferred Contact </h2>

<label> Type </label>
@Html.DropDownListFor(m => m.Applicants[0].PreferredContact.Type, new     List<SelectListItem> {
  new SelectListItem() {Text = "-- Please Select --",  Value=""         },
  new SelectListItem() {Text = "Mobile",               Value="mobile"   },
  new SelectListItem() {Text = "LandLine",             Value="landline" },
  }, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.Applicants[0].PreferredContact.Type)


<label>Prefix</label>
@Html.DropDownListFor(m => m.Applicants[0].PreferredContact.Prefix, new List<SelectListItem> {
  new SelectListItem() {Text = "-- Please Select --",  Value=""      },
  new SelectListItem() {Text = "021",                  Value="021"   },
  new SelectListItem() {Text = "022",                  Value="022"   },
  new SelectListItem() {Text = "025",                  Value="025"   },
  new SelectListItem() {Text = "027",                  Value="027"   },
}, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.Applicants[0].PreferredContact.Prefix)

<label>Number</label>
@Html.TextBoxFor(m => m.Applicants[0].PreferredContact.Number, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.Applicants[0].PreferredContact.Number)

<h2> Secondary Contact </h2>

<label>Contact Type</label>
@Html.DropDownListFor(m => m.Applicants[0].SecondaryContact.Type, new List<SelectListItem> {
   new SelectListItem() {Text = "-- Please Select --",  Value="" },
   new SelectListItem() {Text = "Mobile",               Value="mobile"   },
   new SelectListItem() {Text = "LandLine",             Value="landline" },
 }, new { @class = "form-control" })
 @Html.ValidationMessageFor(m => m.Applicants[0].SecondaryContact.Type)

 <label>Prefix</label>
 @Html.DropDownListFor(m => m.Applicants[0].SecondaryContact.Prefix, new List<SelectListItem> {
    new SelectListItem() {Text = "-- Please Select --",  Value=""      },
    new SelectListItem() {Text = "021",                  Value="021"   },
    new SelectListItem() {Text = "022",                  Value="022"   },
    new SelectListItem() {Text = "025",                  Value="025"   },
    new SelectListItem() {Text = "027",                  Value="027"   },
  }, new { @class = "form-control" })
  @Html.ValidationMessageFor(m => m.Applicants[0].SecondaryContact.Prefix)

  <label>Number</label>
  @Html.TextBoxFor(m => m.Applicants[0].SecondaryContact.Number, new { @class = "form-control" })
  @Html.ValidationMessageFor(m => m.Applicants[0].SecondaryContact.Number)

The only object required is the preferred contact. The secondary contact object is optional. How can I disable both client side and server side validation on the Secondary contact object even though the object class contains data annotations for each of the three fields. These data annotations are more to ensure that at the very least the user provides a preferred contact method.

CodeMonkey
  • 119
  • 4
  • 12
  • Create 2 separate view models, one with the validation attributes and the other without –  Nov 26 '15 at 23:02
  • @Stephen Muecke So different version of the PhoneContact class? – CodeMonkey Nov 26 '15 at 23:05
  • Yes. Say `class RequiredPhoneContactVM` (with the validation attributes) and `class OptionalPhoneContactVM` (without the attributes) and `class PersonVM` with properties `RequiredPhoneContactVM PreferredContact` and `OptionalPhoneContactVM SecondaryContact` –  Nov 26 '15 at 23:08
  • Ok. I'll give it a try. Thankyou. – CodeMonkey Nov 26 '15 at 23:11

0 Answers0