博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MVC中使用RemoteAttribute异步远程验证
阅读量:4504 次
发布时间:2019-06-08

本文共 1098 字,大约阅读时间需要 3 分钟。

使用方法:

1。Model中增加Remote Attribute,并指定相应的验证Action路径

public class UsingRemote    {         [Required]           [Remote("IsNumberEven", "GuestBook", ErrorMessage = "数字必须是偶数!")]           public int EvenNumber { get; set; }    }

IsnumberEven为Action,GuestBook为Controller

2。Controller中创建相应验证方法:

[HttpGet]        public JsonResult IsNumberEven(int EvenNumber)        {            return Json(EvenNumber % 2 == 0, JsonRequestBehavior.AllowGet);        }

注意:必须为[HttpGet],返回结果必须为Json

3.View中添加元素:

@using Mvc4Application.Models@model UsingRemote@{    ViewBag.Title = "RemoteAttribute";}@{Html.EnableClientValidation(); Html.EnableUnobtrusiveJavaScript();}

RemoteAttribute

@using (Html.BeginForm("RemoteAttribute", "GuestBook")){ @Html.EditorForModel() }

注意:因为Remote实际为通过调用JQuery实现的异步远程调用,所以必须在_layout.cshtml中同时引用了如下三个文件:

                  

且在该view中声明了:

@{Html.EnableClientValidation(); Html.EnableUnobtrusiveJavaScript();}

或在web.config中声明:

 

转载于:https://www.cnblogs.com/Finding2013/archive/2013/05/09/3068585.html

你可能感兴趣的文章
前端常见跨域解决方案(全)
查看>>
单点登录(Single Sign On)解决方案
查看>>
umi---className设置多个样式
查看>>
网页包抓取工具Fiddler工具简单设置
查看>>
周总结报告
查看>>
Selecting Courses POJ - 2239(我是沙雕吧 按时间点建边 || 匹配水题)
查看>>
Win+R指令(2)
查看>>
codeforces 578c - weekness and poorness - 三分
查看>>
数值微分方程
查看>>
动态规划--电路布线(circuit layout)
查看>>
<转>OD常用断点列表
查看>>
描边时消除锯齿SetSmoothingMode
查看>>
15回文相关问题
查看>>
将VS2013项目转成VS2010项目的方法
查看>>
[置顶] 怎么对待重复的代码
查看>>
多种方法实现H5网页图片动画效果;
查看>>
Ubuntu/CentOS下使用脚本自动安装 Docker
查看>>
源码解读Mybatis List列表In查询实现的注意事项
查看>>
POJ 2311 Cutting Game(二维SG+Multi-Nim)
查看>>
1978 Fibonacci数列 3
查看>>