Prevent form part validation in asp.net MVC 3

I have a form with 11 fields for the delivery address and 11 fields for the billing address, plus the flag "Delivery address matches billing address". When the checkbox is checked, I use jquery to call .hide () on the div containing the sender's input addresses. How then to disable client and server side validation for delivery address fields?

+4
source share
3 answers

You can try this solution for the RequiredIf attribute in MVC4 (or 3) in

http://cchitsiang.blogspot.com/2011/04/requiredif-conditional-validation-if.html

You can allow the user to submit an incomplete form or a fully validated form based on the check box. Be sure to change the following in RequiredIfAttribute.cs

[AttributeUsage (AttributeTargets.Property, AllowMultiple = true, Inherited = true)]

Regarding the link mentioned above,

For rollback lists to be checked, be sure to do the following in the model.cs file

Add boolean variable IsCompleted at the bottom of all variables

[Display(Name = "Completed")] public bool IsCompleted { get; set; } 

for Dropdownlists to be checked pass DBsets to the ViewBag inside your controller, e.g.

  ViewBag.Relationships = db.Relationships; 

In the view, add dropdown menus as follows

 @Html.DropDownListFor(model => model.RelationshipId, new SelectList(ViewBag.Relationships, "RelationshipId", "RelationshipName"), "--- Select ---") <br /> @Html.ValidationMessageFor(model => model.RelationshipId) 
+1
source

@Brad,

In MVC 2, you need to use the Foolproof authentication available in MVC Contrib, as it was inadequate for model validation. Then you will need to use the RequiredIf Attribute so that the delivery fields are needed only if the check box is selected. You can use jquery to hide / show fields.

MVC 3 has a built-in model validation check, but I think someone else can fill me in here, how to do what you need. Perhaps MVC itself has a RequiredIF attribute.

If you need more help, I'm sure I can even provide additional help and code examples.

0
source

Add a custom attribute, such as [RequiredIf], as described here .

0
source

Source: https://habr.com/ru/post/1338779/


All Articles