MVC 3中的jQuery自定义验证问题 [英] jQuery custom validation problem in MVC 3

查看:106
本文介绍了MVC 3中的jQuery自定义验证问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MVC3.我想在提交页面时在客户端添加自定义检查..但是这样做默认验证不起作用...

I am using MVC3. I want to add a custom checking in client side when submitting the page..But doing so the default validation is not working...

@using (Html.BeginForm())

{

 @Html.TextBoxFor(model => model.Title, new { maxlength = 250 , style = "width:450px;" })
 @Html.ValidationMessageFor(model => model.Title)

 <input type="submit" value="Submit" id="submit"/>

}

当我单击提交"按钮时,我在下面写一个验证码

When i click the submit button I write a validation below

 $('#submit').click(function () {/*code for validation*/ if not satisfied then  return false; else return true; }

这很好,如果返回false,则模型提供的默认验证无效 IE. @Html.ValidationMessageFor(model => model.Title)此代码不起作用[错误消息未显示].如何在单击按钮时验证默认验证?

This is working fine.In case of return false , the default validation provided by model is not working ie. @Html.ValidationMessageFor(model => model.Title) this code is not working[Error message not showing].How to validate the default validation when click the button?

推荐答案

尝试将以下行添加到视图中. Html.EnableClientValidation();

try adding the following line to your view. Html.EnableClientValidation();

检查是否在web.config中启用了客户端验证:

Check that Client validation is enabled in web.config:

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

您将需要引用以下脚本:

You will need to reference the following scripts:

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

此链接将为您提供一些背景知识. http://bradwilson.typepad.com/blog/2010/10 /mvc3-unobtrusive-validation.html

This link will give you a bit of background as to how it works. http://bradwilson.typepad.com/blog/2010/10/mvc3-unobtrusive-validation.html

这篇关于MVC 3中的jQuery自定义验证问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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