MVC3 client validation not working -
i have required annotation on model:
[required(errormessage = "please choose option")] public bool? anydebts { get; set; }
i have enabled client validation in web.config:
<appsettings> <add key="clientvalidationenabled" value="true" /> <add key="unobtrusivejavascriptenabled" value="true" /> </appsettings>
i have referenced jquery scripts in layout:
<script src="@url.content("~/scripts/jquery-1.4.4.min.js")" type="text/javascript"></script> <script src="@url.content("~/scripts/jquery-1.4.4.js")" type="text/javascript"></script> <script src="@url.content("~/scripts/jquery.validate.js")" type="text/javascript"></script> <script src="@url.content("~/scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script> <script src="@url.content("~/scripts/jquery-ui-1.8.6.custom.min.js")" type="text/javascript"></script>
what else need make client validation work? server side validation still working.
edit:
ah ha!
i have found client side validation working.
however, specifically, have found model properties not being validated client side annotated custom attributes. example:
[booleanrequiredtobetrue(errormessage = "you must agree statements listed")] public bool statementagree { get; set; }
the code attribute:
public class booleanrequiredtobetrueattribute: requiredattribute { public override bool isvalid(object value) { return value != null && (bool)value; } }
are these not validated client side anymore?
<script src="@url.content("~/scripts/jquery-1.4.4.js")" type="text/javascript"></script> <script src="@url.content("~/scripts/jquery.validate.js")" type="text/javascript"></script> <script src="@url.content("~/scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
are required scripts client validation work.
and here's full working demo:
model:
public class myviewmodel { [required(errormessage = "please choose option")] public bool? anydebts { get; set; } }
controller:
public class homecontroller : controller { public actionresult index() { return view(new myviewmodel()); } [httppost] public actionresult index(myviewmodel model) { return view(model); } }
view:
@model appname.models.myviewmodel @{ viewbag.title = "home page"; } <script src="@url.content("~/scripts/jquery.validate.js")" type="text/javascript"></script> <script src="@url.content("~/scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script> @using (html.beginform()) { <div>debts</div> <span>yes</span> @html.radiobuttonfor(x => x.anydebts, true) <span>no</span> @html.radiobuttonfor(x => x.anydebts, false) @html.validationmessagefor(x => x.anydebts) <input type="submit" value="ok" /> }
remark: haven't included jquery-1.4.4.js
in view because referenced in layout.
Comments
Post a Comment