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.