1

I looked at this SO post and followed what the first answer did and I did the same but I am still not able to see the error showing up.

The relevant part of the model is:

[Display(Name = "Verify13", ResourceType = typeof(Resources.Resource))]
public bool Is13 { get; set; }

The Controller is:

if (registerModel.Is13 == false)
{
    ModelState.AddModelError("REGISTER_0", "You must be 13 of age or older to receive email updates.");
    return View(registerModel);
}

The part of the view is:

@Html.ValidationSummary(true)
...
@Html.LabelFor(model => model.Is13)
@Html.CheckBoxFor(model => model.Is13)
@Html.ValidationMessage("REGISTER_O")

What am I doing wrong? The rest of the model has attributes on the fields. The entire model is shown below. The error messages (field is required). Every error is showing up except this one.

public class RegisterViewModel
{
    [Display(Name = "FirstName", ResourceType = typeof(Resources.Resource))]
    [Required(ErrorMessageResourceType = typeof(Resources.Resource),
              ErrorMessageResourceName = "FirstNameRequired")]
    [StringLength(50, ErrorMessageResourceType = typeof(Resources.Resource),
                      ErrorMessageResourceName = "FirstNameLong")]
    public string FirstName { get; set; }

    [Display(Name = "LastName", ResourceType = typeof(Resources.Resource))]
    [Required(ErrorMessageResourceType = typeof(Resources.Resource),
              ErrorMessageResourceName = "LastNameRequired")]
    [StringLength(50, ErrorMessageResourceType = typeof(Resources.Resource),
                      ErrorMessageResourceName = "LastNameLong")]
    public string LastName { get; set; }

    [Display(Name = "Email", ResourceType = typeof(Resources.Resource))]
    [Required(ErrorMessageResourceType = typeof(Resources.Resource),
              ErrorMessageResourceName = "EmailRequired")]
    [RegularExpression(".+@.+\\..+", ErrorMessageResourceType = typeof(Resources.Resource),
                                     ErrorMessageResourceName = "EmailInvalid")]
    public string Email { get; set; }

    [Display(Name = "ConfirmEmail", ResourceType = typeof(Resources.Resource))]
    [Required(ErrorMessageResourceType = typeof(Resources.Resource),
            ErrorMessageResourceName = "ConfirmEmailRequired")]
    [RegularExpression(".+@.+\\..+", ErrorMessageResourceType = typeof(Resources.Resource),
                                     ErrorMessageResourceName = "EmailInvalid")]
    public string ConfirmEmailAddress { get; set; }

    [Display(Name = "Password", ResourceType = typeof(Resources.Resource))]
    [Required(ErrorMessageResourceType = typeof(Resources.Resource),
              ErrorMessageResourceName = "PasswordRequired")]
    public string Password { get; set; }

    [Display(Name = "ConfirmPassword", ResourceType = typeof(Resources.Resource))]
    [Required(ErrorMessageResourceType = typeof(Resources.Resource),
              ErrorMessageResourceName = "ConfirmPasswordRequired")]
    public string ConfirmPassword { get; set; }

    [Display(Name = "ZipCode", ResourceType = typeof(Resources.Resource))]
    [Required(ErrorMessageResourceType = typeof(Resources.Resource),
              ErrorMessageResourceName = "ZipCodeRequired")]
    public string ZipCode { get; set; }

    public string[] VideoProvider { get; set; }
    public IEnumerable<SelectListItem> Items { get; set; }

    [Display(Name = "Verify13", ResourceType = typeof(Resources.Resource))]
    public bool Is13 { get; set; }
Community
  • 1
  • 1
user2471435
  • 1,644
  • 7
  • 35
  • 62
  • Your code works fine. Check the html once the view has been returned with the error. You should see `You must be 13 ....`. I suspect you have some `.css` properties preventing it from being displayed. –  Aug 26 '14 at 23:52

2 Answers2

1

Or you could decorate your property.

private bool isTrue
{ get { return true; } }

[Required]
[Display(Name = "I agree to the terms and conditions")]
[Compare("isTrue", ErrorMessage = "Please agree to Terms and Conditions")]
public bool iAgree { get; set; }

This is @fields.cage answer here

This way you have your validation logic where it should be and you're able to bind it like the rest.

EDIT

Below the code that gives the output I think you want to see. I only implemented the firstname and the is13 field for simplicity.

Model:

public class RegisterViewModel
{
    [Display(Name = "FirstName")]
    [Required]
    public string FirstName { get; set; }

    public bool isTrue
    { get { return true; } }

    [Display(Name = "Verify13")]
    [Required]
    [System.Web.Mvc.CompareAttribute("isTrue", ErrorMessage = "You must be 13 of age or     older to receive email updates.")]
    public bool Is13 { get; set; }
}

View:

@using (Html.BeginForm("Index", "Home"))
{
    <table>
        <tr>
            <td>@Html.LabelFor(s => s.FirstName)</td>
            <td>@Html.TextBoxFor(s => s.FirstName)</td>
            <td>@Html.ValidationMessageFor(s => s.FirstName)</td>
        </tr>
        <tr>
            <td>@Html.LabelFor(s => s.Is13)</td>
            <td>@Html.CheckBoxFor(s => s.Is13)</td>
            <td>@Html.ValidationMessageFor(s => s.Is13)</td>
        </tr>
    </table>

    <button type="submit">Send</button>
}

Controller:

[HttpPost]
public ActionResult Index(RegisterViewModel model)
{
    if (!ModelState.IsValid)
    {
        return View(model);
    }

    return View(model);
}
Community
  • 1
  • 1
Wouter
  • 98
  • 5
  • I did that but its not showing up because I am using CheckBoxFor and it is displaying the label on top of the checkbox, not next to it, right where the error message should display – user2471435 Aug 26 '14 at 18:52
  • I edited my answer. I think the table will do the trick when it comes to setting the location of the validation message and labels. – Wouter Aug 27 '14 at 08:21
0

Did you add these 2 javascript files to your view (for client side validation):

~/Scripts/jquery.validate.min.js
~/Scripts/jquery.validate.unobtrusive.min.js

And if you are using Ajax.BeginForm( ) function you will also need this file:

~/Scripts/jquery.unobtrusive-ajax.min.js
Cacho Santa
  • 6,846
  • 6
  • 41
  • 73
  • What is the syntax of adding the first two to the form? And why do the Required attributed fields show an error if not there? – user2471435 Aug 26 '14 at 16:24
  • just add the files as any [javascript file](http://stackoverflow.com/questions/4593682/include-javascript-file-in-html-wont-work-as-script). – Cacho Santa Aug 26 '14 at 16:27
  • That doesn't work. All the other error messages for required fields such as the way I set up ZipCode show up. All fields except this one – user2471435 Aug 26 '14 at 16:32