MVC validate.js下接纳 ajaxSubmit

 

网上检索基本上都以

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

图片 1

前端

 

 

不过此格局根本就不只怕跻身,因为jquery.validate.unobtrusive.min.js已经重写了jquery.validate.min.js的validate方法

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 < 18)
            {
                return Json(true, JsonRequestBehavior.AllowGet);
            }
            return Json("Error", JsonRequestBehavior.AllowGet);
        }
    }
}

 

本文版权归作者和网易共有,欢迎转发,但未经作者同意必须保留此段评释,且在篇章页面彰着地方给出原文连接。

1.高效支付

 

帮你们化解难题的,点个赞

实现Ajax

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

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

 

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

图片 2

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

首页定义表达实体

行使Mvc + jquery.validate.js
验证好处:

@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" />
} 

2.支撑后台验证 [Remote(“CheckAge”, “Home”)]

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

相关文章