Implementing Remote Validation in MVC
Using Validation Code
Step 1: Create model for Catalog table and apply the the remote validation for the column that must be validated on client side.
Step 2: Write a method in controller to check the validation for that column. You can also send the additional parameters by adding AdditionFields attribute.

- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.ComponentModel.DataAnnotations;
- using System.Web.Mvc;
- namespace ItemCatalog.Models
- {
- public class Catalog
- {
- [Required]
- public long Id { get; set; }
- [Required]
- [Display(Name = "Item Name")]
- public string CatalogName { get; set; }
- [Required]
- [Display(Name = "Bar code")]
- [Remote("IsBarCodeUnique","Catalog",AdditionalFields="CatalogName",ErrorMessage="This {0} is already used.")]
- public string Barcode { get; set; }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using ItemCatalog.Models;
- namespace ItemCatalog.Controllers
- {
- public class CatalogController : Controller
- {
- //
- // GET: /Catalog/
- public ActionResult Catalog()
- {
- Catalog catalog = new Catalog();
- return View(catalog);
- }
- public JsonResult SaveCatalog(Catalog catalog)
- {
- // Action to save the data
- return Json(true);
- }
- public JsonResult IsBarCodeUnique(Catalog catalog)
- {
- return IsExist(catalog.CatalogName, catalog.Barcode)
- ? Json(true, JsonRequestBehavior.AllowGet)
- : Json(false, JsonRequestBehavior.AllowGet);
- }
- public bool IsExist(string catalogName, string barcode)
- {
- //True:False--- action that implement to check barcode uniqueness
- return false;//Always return false to display error message
- }
- }
- }
- @model ItemCatalog.Models.Catalog
- @{
- ViewBag.Title = "Catalog";
- Layout = "~/Views/Shared/_Layout.cshtml";
- }
- @section scripts {
- <style type="text/css">
- .row
- {
- float: left;
- width: 100%;
- padding: 10px;
- }
- .row label
- {
- width: 100px;
- float: left;
- }
- #success-message
- {
- color: Green;
- }
- </style>
- <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
- <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
- <script type="text/javascript">
- function SaveCatalogComplete(result) {
- $("#success-message").show();
- }
- </script>
- }
- <h2>
- Item</h2>
- @using (Ajax.BeginForm("SaveCatalog", new AjaxOptions { HttpMethod = "POST", OnSuccess = "SaveCatalogComplete" }))
- {
- <fieldset>
- <div class="row">
- @Html.LabelFor(x => x.CatalogName)
- @Html.TextBoxFor(x => x.CatalogName, Model.CatalogName)
- @Html.ValidationMessageFor(x => x.CatalogName)
- </div>
- <div class="row">
- @Html.LabelFor(x => x.Barcode)
- @Html.TextBoxFor(x => x.Barcode, Model.Barcode)
- @Html.ValidationMessageFor(x => x.Barcode)
- </div>
- </fieldset>
- <div id="success-message" style="display: none;">
- This record is successfully saved!!
- </div>
- <div>
- <input type="submit" value="Save" />
- </div>
- }
Step 3: Return the JsonResult object as per validation response.
Summary :
It's easy to implement and gives the same type of error message results without writing any Ajax to call server side validation.
Implementing Remote Validation in MVC的更多相关文章
- MVC学习系列13--验证系列之Remote Validation
大多数的开发者,可能会遇到这样的情况:当我们在创建用户之前,有必要去检查是否数据库中已经存在相同名字的用户.换句话说就是,我们要确保程序中,只有一个唯一的用户名,不能有重复的.相信大多数人都有不同的解 ...
- 转载:Remote Validation
http://www.jb51.net/article/89474.htm 大多数的开发者,可能会遇到这样的情况:当我们在创建用户之前,有必要去检查是否数据库中已经存在相同名字的用户.换句话说就是,我 ...
- asp net core Remote Validation 无法验证
[注意这里,Remote Validation是需要引入Jquery插件和启用客户端验证的]
- Model Validation in Asp.net MVC
原文:Model Validation in Asp.net MVC 本文用于记录Pro ASP.NET MVC 3 Framework中阐述的数据验证的方式. 先说服务器端的吧.最简单的一种方式自然 ...
- [引]ASP.NET MVC 4 Content Map
本文转自:http://msdn.microsoft.com/en-us/library/gg416514(v=vs.108).aspx The Model-View-Controller (MVC) ...
- MVC学习系列4--@helper辅助方法和用户自定义HTML方法
在HTML Helper,帮助类的帮助下,我们可以动态的创建HTML控件.HTML帮助类是在视图中,用来呈现HTML内容的.HTML帮助类是一个方法,它返回的是string类型的值. HTML帮助类, ...
- Asp.net MVC 版本简史
http://www.dotnet-tricks.com/Tutorial/mvc/XWX7210713-A-brief-history-of-Asp.Net-MVC-framework.html A ...
- Fluent Validation + NInject3 + MVC5
Fluent Validation + NInject + MVC - Why & How : Part 1 http://fluentvalidation.codeplex.com/ htt ...
- Asp.net mvc 知多少(一)
本系列主要翻译自<ASP.NET MVC Interview Questions and Answers >- By Shailendra Chauhan,想看英文原版的可访问http:/ ...
随机推荐
- MySql的安装与使用
今天因为毕业设计要用到MySql数据库,所以就准备自己安装一个MySQL数据库,但是因为MySQL Install MSI只有32位,所以最后选择使用Windows (x86, 64-bit), ZI ...
- linux svn authorization failed错误
authorization failed错误主要是conf/auth文件配置错误,可以参考如下配置: [aliases] # joe = /C=XZ/ST=Dessert/L=Snake City/O ...
- socket 基础学习
这个示例程序是同步套接字程序,功能很简单,只是客户端发给服务器一条信息,服务器向客户端返回一条信息:这里只是一个简单的示例,是一个最基本的socket编程流程,在接下来的文章中,会依次记录套接字的同步 ...
- python BeautifulSoup find 方法
这里我们重点讲一下find的几种用法,其他的类比: find(name=None, attrs={}, recursive=True, text=None, **kwargs) (ps:只讲几种用法, ...
- Cassandra1.2文档学习(4)——分区器
参考文档:http://www.datastax.com/documentation/cassandra/1.2/webhelp/index.html#cassandra/architecture/a ...
- php获取汉字首字母的函数
本文介绍用php实现汉字转化为首字母的方法,主要功能是:功能明确,易于修改维护和扩展: 英文的字串:不变返回(包括数字):中文字符串:返回拼音首字符: 中英混合串: 返回拼音首字符和英文. 网上的方法 ...
- 解决IE 下div与img重叠无法触发鼠标事件的问题
在IE下当我想在img标签上层显示一个div元素时,此时如果该div的background为空白(没有设置图片.或者颜色填充),会导致该div的鼠标事件失效:如果设置border为1px solid ...
- Python环境搭建(windows)
Python环境搭建(windows) Python简介 Python(英国发音:/ˈpaɪθən/ 美国发音:/ˈpaɪθɑːn/),是一种面向对象.直译式计算机编程语言,具有近二十年的发展历史,成 ...
- Python用format格式化字符串
format是是python2.6新增的一个格式化字符串的方法,相对于老版的%格式方法,它有很多优点. 1.不需要理会数据类型的问题,在%方法中%s只能替代字符串类型 2.单个参数可以多次输出,参数顺 ...
- Linux命令总结(转载)
转子:http://www.cnblogs.com/CGDeveloper/archive/2011/05/27/2060009.html 昨天看了一个教程,关于Linux命令的,本来以为当是复习随便 ...