2

I want to make checkboxes optional, whether to be checked or not. But when I add a checkbox on my view it will make unobtrusive validation to be stopped. But when I remove the checkbox from my view, unobtrusive validations works fine. What's the problem ? I am adding checkbox like this:

@Html.CheckBoxFor(model => model.features.safetyfeature.Airbag)

All the model values are correct.

1
  • Same problem here. I noticed that by default, it adds the data-val-required and data-val attributes to the checkbox by default. When those are there, the problems you describe happen. When I remove them using the inspector, the form validates as desired. In summary, it looks like CheckBoxFor adds the required attributes automatically. Commented Aug 24, 2016 at 22:11

1 Answer 1

1

I had exactly this problem & posted a question here. Currently I don't have a solution to the problem, but I did find a workaround. Instead of using Html.CheckBoxFor I used Html.CheckBox like so:

@Html.CheckBox("Employee.Active")

I gave it the same name as the CheckBoxFor that was created by the razor view. This is now working as I expected but I consider it a workaround rather that a solution, and I have no idea what's actually causing the problem!

Sign up to request clarification or add additional context in comments.

1 Comment

This worked for me. It's not ideal but it solves the problem and I can live with it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.