不工作MVC3客户端验证 [英] MVC3 Client side validation not working

查看:83
本文介绍了不工作MVC3客户端验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用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>  

我的表是这样的:

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>
}

我的视图模型有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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆