MVC3 客户端验证不起作用 [英] MVC3 Client side validation not working
问题描述
我在 Razor 中使用 MVC3.
我在 _Layout.cshtml 中包含了以下内容:
I'm using MVC3 with Razor.
I've included the following in my _Layout.cshtml:
<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/MicrosoftMvcAjax.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/MicrosoftMvcValidation.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/MicrosoftAjax.js")" type="text/javascript"></script>
我的表单看起来像:
@{
ViewBag.Title = "Register";
Html.EnableClientValidation();
}
@using (Html.BeginForm("Register"))
{
<fieldset>
<p>
@Html.LabelFor(o => o.Email)
@Html.TextBoxFor(o => o.Email)
@Html.ValidationMessageFor(o => o.Email)
</p>
...
</fieldset>
}
我的 ViewModel 具有 DataAnnotations(并实现 IValidatableObject),并且它在控制器操作期间进行验证.但是,如果不发布表单,我似乎无法在客户端使用 JS 验证.
My ViewModel has DataAnnotations (and implements IValidatableObject), and it validates during controller action. However I cannot seem to be able to use JS validation on the clientside without posting the form.
我错过了什么?
推荐答案
在 ASP.NET MVC 3 中,jquery 验证插件是执行客户端验证的默认插件.因此,您可以从项目中删除所有 Microsoft*.js
脚本.您只需要以下内容:
In ASP.NET MVC 3 the jquery validation plugin is the default for performing client-side validation. So you could remove all Microsoft*.js
scripts from your project. You only need the following:
<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>
并删除 Html.EnableClientValidation();
调用.在 web.config 中启用客户端验证:
and remove the Html.EnableClientValidation();
call. Client validation is enabled in web.config:
<appSettings>
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
这篇关于MVC3 客户端验证不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!