简体   繁体   English

MVC3客户端验证不起作用

[英]MVC3 Client side validation not working

I'm using MVC3 with Razor. 我正在使用带Razor的MVC3。
I've included the following in my _Layout.cshtml: 我在_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. 我的ViewModel具有DataAnnotations(并实现IValidatableObject),并在控制器操作期间进行验证。 However I cannot seem to be able to use JS validation on the clientside without posting the form. 但是,如果不发布表单,我似乎无法在客户端使用JS验证。

What am I missing? 我错过了什么?

In ASP.NET MVC 3 the jquery validation plugin is the default for performing client-side validation. 在ASP.NET MVC 3中,jquery验证插件是执行客户端验证的默认设置。 So you could remove all Microsoft*.js scripts from your project. 因此,您可以从项目中删除所有Microsoft*.js脚本。 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>

and remove the Html.EnableClientValidation(); 并删除Html.EnableClientValidation(); call. 呼叫。 Client validation is enabled in web.config: 在web.config中启用了客户端验证:

<appSettings>
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>

Remove the Html.EnableClientValidation(); 删除Html.EnableClientValidation(); and goto your web config, and make sure you have an appSetting that looks like the below: 并转到您的网络配置,并确保您有一个如下所示的appSetting:

<add key="ClientValidationEnabled" value="true"/>

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. 然后我在前端输入0,之后客户端验证工作完美无瑕。 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. 我想验证是否适用于初始空白文本框...验证似乎不正确,等待尝试的错误值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM