问题描述
我有一个要求标注在我的模型:
[必需(的ErrorMessage =请选择一个选项)]
公共BOOL? AnyDebts {搞定;组; }
在web.config
我已经启用客户端验证:
<&的appSettings GT;
<添加键=ClientValidationEnabledVALUE =真/>
<添加键=UnobtrusiveJavaScriptEnabledVALUE =真/>
< /的appSettings>
我在我的布局引用的jQuery的脚本:
<脚本的src =@ Url.Content(〜/脚本/ jQuery的-1.4.4.min.js)TYPE =文/ JavaScript的>&LT ; / SCRIPT>
<脚本的src =@ Url.Content(〜/脚本/ jQuery的-1.4.4.js)TYPE =文/ JavaScript的>< / SCRIPT>
<脚本的src =@ Url.Content(〜/脚本/ jquery.validate.js)TYPE =文/ JavaScript的>< / SCRIPT>
<脚本的src =@ Url.Content(〜/脚本/ jquery.validate.unobtrusive.js)TYPE =文/ JavaScript的>< / SCRIPT>
<脚本的src =@ Url.Content(〜/脚本/ jQuery的-UI-1.8.6.custom.min.js)TYPE =文/ JavaScript的>< / SCRIPT>
还有什么我需要做的,使客户端验证工作的?
服务器端验证仍在工作。
编辑:
啊哈!
我发现客户端验证工作。
然而,具体而言,我发现,模型性能没有被验证客户端是那些标注有自定义属性。
例如:
[BooleanRequiredToBeTrue(的ErrorMessage =你必须同意列出的声明)
公共BOOL StatementAgree {搞定;组; }
在code的属性:
公共类BooleanRequiredToBeTrueAttribute:RequiredAttribute标签
{
公众覆盖BOOL的IsValid(对象的值)
{
返回值= NULL&放大器;!&安培; (布尔)值;
}
}
难道这些不验证客户端了吗?
<脚本的src =@ Url.Content(〜/脚本/ jQuery的-1.4.4.js )TYPE =文/ JavaScript的>< / SCRIPT>
<脚本的src =@ Url.Content(〜/脚本/ jquery.validate.js)TYPE =文/ JavaScript的>< / SCRIPT>
<脚本的src =@ Url.Content(〜/脚本/ jquery.validate.unobtrusive.js)TYPE =文/ JavaScript的>< / SCRIPT>
是唯一需要的脚本客户端验证工作。
和一如既往这里有一个完整的工作演示:
型号:
公共类MyViewModel
{
[必需(的ErrorMessage =请选择一个选项)]
公共BOOL? AnyDebts {搞定;组; }
}
控制器:
公共类HomeController的:控制器
{
公众的ActionResult指数()
{
返回查看(新MyViewModel());
} [HttpPost]
公众的ActionResult指数(MyViewModel模型)
{
返回查看(模型);
}
}
查看:
@model AppName.Models.MyViewModel
@ {
ViewBag.Title =主页;
}
<脚本的src =@ Url.Content(〜/脚本/ jquery.validate.js)TYPE =文/ JavaScript的>< / SCRIPT>
<脚本的src =@ Url.Content(〜/脚本/ jquery.validate.unobtrusive.js)TYPE =文/ JavaScript的>< / SCRIPT>
@using(Html.BeginForm())
{
< DIV>&债务LT; / DIV>
<跨度>是< / SPAN> @ Html.RadioButtonFor(X => x.AnyDebts,真)
<跨度&GT否LT; / SPAN> @ Html.RadioButtonFor(X => x.AnyDebts,FALSE) @ Html.ValidationMessageFor(X => x.AnyDebts)
<输入类型=提交VALUE =OK/>
}
注:我没有包含 jQuery的-1.4.4.js
在我看来,因为它是在布局已经引用
I have a required annotation on my model:
[Required(ErrorMessage = "Please choose an option")]
public bool? AnyDebts { get; set; }
I have enabled client validation in the web.config:
<appSettings>
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
I have referenced the jquery scripts in my 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 do I need to do to make client validation work?Server side validation is still working.
EDIT:
Ah ha!
I have found that client side validation is working.
However, specifically, I have found that model properties are not being validated client side are those annotated with custom attributes.For example:
[BooleanRequiredToBeTrue(ErrorMessage = "You must agree to the statements listed")]
public bool StatementAgree { get; set; }
The code for the 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 the only required scripts for client validation to work.
And as always here's a full working demo:
Model:
public class MyViewModel
{
[Required(ErrorMessage = "Please choose an 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: I haven't included jquery-1.4.4.js
in my view because it is already referenced in the layout.
这篇关于不工作MVC3客户端验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!