0

I have the following model:

public class Model1
{
    [Required(ErrorMessage="E1")]
    public string Name { get; set; }
    [Required(ErrorMessage="E2")]
    [RegularExpression(".+\\@.+\\..+")]
    public string Email { get; set; }
    [Required(ErrorMessage="E3")]
    public bool WillAttend { get; set; }
}

controller action:

    public ActionResult Model1()
    {
        Model1 m = new Model1();
        return View(m);
    }

and view:

@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Model1</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.WillAttend, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                <div class="checkbox">
                    @Html.EditorFor(model => model.WillAttend)
                    @Html.ValidationMessageFor(model => model.WillAttend, "", new { @class = "text-danger" })
                </div>
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

problem is required validator for WillAttend property does not work. Even in action method, ModelState.IsValid is true. Why and how to do WillAttend is required ?

John
  • 727
  • 2
  • 10
  • 17
  • 1
    When do you expect the Required validator to fire? A Bool only has two possible values (True and False) and both fulfil the requirement for the WillAttend property to have a value. Have a look at the question [Boolean value of True for required attribute on MVC .net property](http://stackoverflow.com/questions/7930808/boolean-value-of-true-for-required-attribute-on-mvc-net-property). – Jens R. Jun 28 '15 at 19:02

1 Answers1

1

At the moment, your required-attribute just checks whether a value was specified. Since a boolean is either true or false, the validation will never fail.

You can, mark the boolean as nullable:

public bool? WillAttend { get; set; }

or you can try to make a custom validator if you are trying to force them to check the box, such as in this link.

Brad C
  • 2,868
  • 22
  • 33