Mvc Foolproof not working on mvc3

1.4k views Asked by At

I have an MVC3 application.

This is the model:

public class Customer
{
    [Required]
    public string Email { get; set; }

    [Required]
    public string Answer1 { get; set; }

    [Required]
    [NotEqualTo("Answer1")]
    public string Answer2 { get; set; }
}

I enabled the onubtrusive validation on the web.config:

<add key="ClientValidationEnabled" value="true"/>
<add key="UnobtrusiveJavaScriptEnabled" value="true"/>

And this on my _layout.cshtml:

<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/modernizr-1.7.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

<script src="@Url.Content("~/Scripts/mvcfoolproof.unobtrusive.js")" type="text/javascript"></script>

My problem here is the MvcFoolproof is not working. The mvc data annotation validation Required is working. What did I missed here? Thanks!

1

There are 1 answers

0
madborn On

First of all, I suppose you have included using Foolproof; in your model.

Second, I believe you might need this script too (for client-side validation to work properly) <script src="/Scripts/MvcFoolproofJQueryValidation.min.js"></script>.

Third, did you add the Foolproof package using NuGet? Your packages.config should contain something like <package id="foolproof" version="0.9.4517" targetFramework="net45" />.

Also, are you displaying any validation messages in the view? Are these being shown for the required properties, but not for the NotEqualTo attribute?

Last, are you displaying all those properties in the view correctly, using e.g. @Html.EditorFor(model => model.Answer1)? Unless Answer2 has something to compare to, it surely won't validate. And you're using the jQuery validate-call on the form?

Mostly shots in the dark here, but getting Foolproof working is usually just a click-and-go operation.