首页定义验证实体

using System.ComponentModel.DataAnnotations;
using System.Web.Mvc; namespace MvcApplication1.Models
{
public class Student
{
[Display(Name = "名称")]
[Required(AllowEmptyStrings = false, ErrorMessage = "输入名称")]
public string Name { get; set; } [Display(Name = "Age")]
[Required(AllowEmptyStrings = false, ErrorMessage = "输入年龄")]
[Remote("CheckAge", "Home")]
public int Age { get; set; }
}
}

编写Controller

using MvcApplication1.Models;
using System.Web.Mvc; namespace MvcApplication1.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
} [HttpPost]
public ContentResult Index(Student student)
{
return Content("success");
} public JsonResult CheckAge(int age)
{
if (age < )
{
return Json(true, JsonRequestBehavior.AllowGet);
}
return Json("Error", JsonRequestBehavior.AllowGet);
}
}
}

前端

@model MvcApplication1.Models.Student

@{
ViewBag.Title = "index";
} <h2>index</h2>
<script src="~/script/jquery.min.js"></script>
<script src="~/script/jquery.validate.min.js"></script>
<script src="~/script/jquery.validate.unobtrusive.min.js"></script>
<script src="~/script/jquery.form.js"></script>
@using (Html.BeginForm(null, null, FormMethod.Post, new { name = "from1", id = "from1" }))
{
@Html.TextBoxFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
@Html.TextBoxFor(model => model.Age)
@Html.ValidationMessageFor(model => model.Age)
<input type="submit" value="提交" id="js_button" />
}

采用Mvc + jquery.validate.js 验证好处:

1.快速开发

2.支持后台验证 [Remote("CheckAge", "Home")]

3.不用在脚本中写入验证规rules如:

        $("#from1").validate({
rules: {
Name: { required: true, maxlength: },
Age: { required: true, maxlength: }
}
});

实现Ajax

网上查找基本上都是

   $(document).ready(function () {
$("#from1").validate({
submitHandler: function (form) {
form.submit();
}
})
})

但是此方法根本就不能进入,因为jquery.validate.unobtrusive.min.js已经重写了jquery.validate.min.js的validate方法

看了源码

b.valid()  b是个form 所以果断修改验证代码如下,亲测成功。

帮你们解决问题的,点个赞

作者:释迦苦僧  出处:http://www.cnblogs.com/woxpp/p/5791296.html

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。

MVC validate.js下使用 ajaxSubmit的更多相关文章

  1. jquery.validate.js在IE8下报错不运行

    因为项目要表单验证,所以特意去找了jquery.validate这个应该是jquery官方团队出品的表单验证插件. 实际应用时,却在IE8调试时碰到了问题. 从jquery.validate官网下载了 ...

  2. Asp.Net 服务器控件下使用Jquery.validate.js

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. jQuery验证控件jquery.validate.js使用说明

    官网地址:http://bassistance.de/jquery-plugins/jquery-plugin-validation jQuery plugin: Validation 使用说明 转载 ...

  4. jquery.validate.js插件使用

    jQuery验证控件jquery.validate.js使用说明+中文API 官网地址:http://bassistance.de/jquery-plugins/jquery-plugin-valid ...

  5. jQuery表单验证插件——jquery.validate.js

    官网地址:http://bassistance.de/jquery-plugins/jquery-plugin-validation 一.导入js库 <script src="../j ...

  6. jQuery插件之验证控件jquery.validate.js

    今天学习一下jQuery.Validate插件,为便于日后翻阅查看和广大博客园园友共享,特记于此. 本博客转载自:jQuery Validate jQuery Validate 插件为表单提供了强大的 ...

  7. Jquery.validate.js表单验证插件的使用

    作为一个网站web开发人员,以前居然不知道还有表单验证这样好呀的插件,还在一行行写表单验证,真是后悔没能早点知道他们的存在. 最近公司不忙,自己学习一些东西的时候,发现了validation的一个实例 ...

  8. aspx中的表单验证 jquery.validate.js 的使用 以及 jquery.validate相关扩展验证(Jquery表单提交验证插件)

    这一期我们先讲在aspx中使用 jquery.validate插件进行表单的验证, 关于MVC中使用 validate我们在下一期中再讲     上面是效果,下面来说使用步骤 jQuery.Valid ...

  9. (转)jQuery验证控件jquery.validate.js使用说明+中文API

    官网地址:http://bassistance.de/jquery-plugins/jquery-plugin-validation jQuery plugin: Validation 使用说明 转载 ...

随机推荐

  1. python学习5 常用三方模块

    watchdog: 监控指定目录/文件的变化并自定义处理事件 例子

  2. Trigger和ViewStateManager的具体比较

    ViewStateManager的好处  拥有 GeneratedDuration ,可以很方便的进行几个状态之间的切换过渡动画. 坏处是,在界面加载时只能显示默认效果,通过GoToStateActi ...

  3. 使用极光推送(www.jpush.cn)向安卓手机推送消息【服务端向客户端主送推送】C#语言

    在VisualStudio2010中新建网站JPushAndroid.添加引用json帮助类库Newtonsoft.Json.dll. 在web.config增加appkey和mastersecret ...

  4. Codeforces CF#628 Education 8 F. Bear and Fair Set

    F. Bear and Fair Set time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  5. Lua笔记

    闭包 示例一 function newCounter() return function() -- anonymous function i = i + return i end end c1 = n ...

  6. python常用模块json、os、sys

    一.序列化 json & pickle 模块 json--用于字符串和Python数据类型间进行转换 pickle---用于python特有的类型和Python的数据类型间进行转换 json: ...

  7. Nodejs windows的安装

    0.下载地址: http://nodejs-org.qiniudn.com/ https://nodejs.org/download/ https://nodejs.org/en/ 1.基本就是一路N ...

  8. uva12063数位dp

    辣鸡军训毁我青春!!! 因为在军训,导致很长时间都只能看书yy题目,而不能溜到机房鏼题 于是在猫大的帮助下我发现这道习题是数位dp 然后想起之前讲dp的时候一直在补作业所以没怎么写,然后就试了试 果然 ...

  9. WEB应用的组成结构

  10. 案例1.用Ajax实现用户名的校验

    用Ajax实现用户名的校验 java的验证类 public class UserDao { public boolean checkUserName(String name) { //这里的name是 ...