Reputation: 709
Server side validation works fine(once i submit the form) but client side doesnt work.
In my layout :
@Scripts.Render("~/bundles/jqueryval")
bundle:
bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
"~/Scripts/jquery-{version}.js",
"~/Scripts/jquery.validate.js",
"~/Scripts/jquery.validate.unobtrusive.js"));
In my config file:
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
I am using Data annotations in my model & MVC 5
VM:
public class LocationWorkingHoursVM
{
public int LocationID { get; set; }
public string Name { get; set; }
[Range(0,1)]
public decimal?[] Hours { get; set; }
public string BankHolidays { get; set; }
}
It is rendered as :
<input class="form-control" data-val="true" data-val-number="The field Nullable`1 must be a number." id="Hours_0_" name="Hours[0]" type="text" value="8.0" />
<span class="field-validation-valid" data-valmsg-for="Hours" data-valmsg-replace="true"></span>
What am i missing?
Upvotes: 0
Views: 625
Reputation: 709
It was because I was using my own template for EditorFor helper. However I had it defined as
TextBoxFor(r=>r.model, new { @class= "foo")})
is it possible to make it work using my template ?
Upvotes: 1
Reputation: 1407
If you are using jQuery 1.9.1 or later version just make sure you are using
jQuery Validation (at least 1.11.1) Microsoft jQuery Unobtrusive Validation (at least 2.0.30116.0) Microsoft jQuery Unobtrusive Ajax (at least 2.0.30116.0)
MVC data annotations range validation not working properly
Upvotes: 0
Reputation:
Try adding these two lines to your mainlayout and try again
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.12.0/jquery.validate.min.js" type="text/javascript"></script>
<script src="http://ajax.aspnetcdn.com/ajax/mvc/4.0/jquery.validate.unobtrusive.min.js" type="text/javascript"></script>
Upvotes: 0