1

I know it is been answered before and there is lot of examples, but I just can not understand them and make them work. I work with a friend on project, but know he do not have time to explain how to do it.

I have this code on my model:

[Required(ErrorMessage = "You must accepted terms")]
[Display(Name = "Is Approve")]
public bool IsApprove { get; set; }

and this code on my view:

@Html.CheckBoxFor(m => m.IsApprove)<br /><br />
@Html.ValidationMessageFor(m => m.IsApprove)

I also have some Controllers

I want to show message for the user that he must accepted the terms after he submit the form if the checkbox is unchecked and submit the form if the checkbox is checked.

Is there some step by step guide to show how to this? I know from asp.net that I can do checks with Javascript or in the code behind, but I can not find out where to it on MVC

tereško
  • 58,060
  • 25
  • 98
  • 150
hanan-mstudio
  • 172
  • 1
  • 2
  • 15

1 Answers1

1

The required attribute only validates whether a property has a value or not. In case of a checkbox there are two possible values true and false.

So the required attribute does not enforce true, it just enforces either true or false, which is always the case. You need a different attribute for that. Unfortunately there's none built-in in asp.net mvc.

You can take a look at the answers in this question: How to handle Booleans/CheckBoxes in ASP.NET MVC 2 with DataAnnotations?

Community
  • 1
  • 1
Kenneth
  • 28,294
  • 6
  • 61
  • 84
  • I have tried the first answer, I see the checkbox but after I submit without checking it, I do not see the error message – hanan-mstudio Jan 19 '14 at 14:30