javascript MVC3 客户端验证不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4706174/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MVC3 Client side validation not working
提问by TDaver
I'm using MVC3 with Razor.
I've included the following in my _Layout.cshtml:
我在 Razor 中使用 MVC3。
我在 _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>
My form looks like:
我的表格看起来像:
@{
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>
}
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.
我的 ViewModel 有 DataAnnotations(并实现 IValidatableObject),它在控制器操作期间进行验证。但是,如果不发布表单,我似乎无法在客户端使用 JS 验证。
What am I missing?
我错过了什么?
回答by Darin Dimitrov
In ASP.NET MVC 3 the jquery validation plugin is the default for performing client-side validation. So you could remove all Microsoft*.jsscripts from your project. You only need the following:
在 ASP.NET MVC 3 中,jquery 验证插件是执行客户端验证的默认插件。因此,您可以Microsoft*.js从项目中删除所有脚本。您只需要以下内容:
<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>
and remove the Html.EnableClientValidation();call. Client validation is enabled in web.config:
并删除Html.EnableClientValidation();呼叫。在 web.config 中启用客户端验证:
<appSettings>
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
回答by jsteve81
Remove the Html.EnableClientValidation(); and goto your web config, and make sure you have an appSetting that looks like the below:
删除 Html.EnableClientValidation(); 并转到您的网络配置,并确保您有一个如下所示的 appSetting:
<add key="ClientValidationEnabled" value="true"/>
回答by zo-
Try adding the following lines to your view.
尝试将以下行添加到您的视图中。
HtmlHelper.ClientValidationEnabled = true;
HtmlHelper.UnobtrusiveJavaScriptEnabled = true;
Along with including jquery-1.4.4.js, jquery.validate.js, jquery.validate.unobtrusive.js
随着包括 jquery-1.4.4.js, jquery.validate.js, jquery.validate.unobtrusive.js
I get it to work for my app but it seems not to kick in until I put in a bad value. For example
我让它为我的应用程序工作,但在我输入错误值之前它似乎不会启动。例如
[Required(ErrorMessageResourceType = typeof(Resources.WValidation), ErrorMessageResourceName = "TestCountRequired")]
[Range(1, Int32.MaxValue, ErrorMessageResourceType = typeof(Resources.WValidation), ErrorMessageResourceName = "TestCountRange")]
public int? TestCountThreshold { get; set; }
then i put in a 0 on the front-end and after that client side validation works flawlessly. I would like for validation to work for initial blank text boxes... It seems incorrect for the validation to wait until there is an attempted bad value.
然后我在前端输入 0,然后客户端验证工作完美无缺。我希望验证适用于初始空白文本框......验证等待直到尝试错误值似乎不正确。

