0

This is whats in my model:

    public CheckBoxListItem Same { get; set; }  

    [Required(ErrorMessage = "* Billing Name field is required")]
    public string BillingName { get; set; }

This is what is in my view:

            @Html.LabelFor(model => model.Same.IsChecked, "Billing Is Same As Shipping")
            @Html.HiddenFor(model => model.Same.Text)
            @Html.CheckBoxFor(model => model.Same.IsChecked)
        </div> 
        <h3>Billing Information</h3>
        <div>
            @Html.LabelFor(model => model.BillingName, "Name")
            @Html.EditorFor(model => model.BillingName)
            @Html.ValidationMessageFor(model => model.BillingName)  
        </div>

When the checkbox is checked, I want the required validation for BillingName to no longer be required when the form is submitted. How do I go about doing this in MVC?

Tommy
  • 9
  • 6
  • Have you checked this link: [http://stackoverflow.com/q/5367287/1351076](http://stackoverflow.com/q/5367287)? – krlzlx Feb 03 '17 at 14:57
  • Using Jquery you can check if the checkbox is checked or not. If checked remove `input-validation-error` class from the field – Ghanshyam Singh Feb 03 '17 at 15:28
  • Consider using a [foolproof](http://foolproof.codeplex.com/) `[RequiredIf]` or similar conditional validation attribute. Or if you want to write your own - refer [The Complete Guide To Validation In ASP.NET MVC 3 - Part 2](http://www.devtrends.co.uk/blog/the-complete-guide-to-validation-in-asp.net-mvc-3-part-2) –  Feb 03 '17 at 21:46

0 Answers0