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.

Hide   Shrink    Copy Code
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.ComponentModel.DataAnnotations;
  6. using System.Web.Mvc;
  7.  
  8. namespace ItemCatalog.Models
  9. {
  10. public class Catalog
  11. {
  12. [Required]
  13. public long Id { get; set; }
  14.  
  15. [Required]
  16. [Display(Name = "Item Name")]
  17. public string CatalogName { get; set; }
  18.  
  19. [Required]
  20. [Display(Name = "Bar code")]
  21. [Remote("IsBarCodeUnique","Catalog",AdditionalFields="CatalogName",ErrorMessage="This {0} is already used.")]
  22. public string Barcode { get; set; }
  23. }
  24. }
  25. using System;
  26. using System.Collections.Generic;
  27. using System.Linq;
  28. using System.Web;
  29. using System.Web.Mvc;
  30. using ItemCatalog.Models;
  31.  
  32. namespace ItemCatalog.Controllers
  33. {
  34. public class CatalogController : Controller
  35. {
  36. //
  37. // GET: /Catalog/
  38.  
  39. public ActionResult Catalog()
  40. {
  41. Catalog catalog = new Catalog();
  42. return View(catalog);
  43. }
  44.  
  45. public JsonResult SaveCatalog(Catalog catalog)
  46. {
  47. // Action to save the data
  48. return Json(true);
  49. }
  50.  
  51. public JsonResult IsBarCodeUnique(Catalog catalog)
  52. {
  53. return IsExist(catalog.CatalogName, catalog.Barcode)
  54. ? Json(true, JsonRequestBehavior.AllowGet)
  55. : Json(false, JsonRequestBehavior.AllowGet);
  56. }
  57.  
  58. public bool IsExist(string catalogName, string barcode)
  59. {
  60. //True:False--- action that implement to check barcode uniqueness
  61.  
  62. return false;//Always return false to display error message
  63. }
  64. }
  65. }
  66. @model ItemCatalog.Models.Catalog
  67. @{
  68. ViewBag.Title = "Catalog";
  69. Layout = "~/Views/Shared/_Layout.cshtml";
  70. }
  71. @section scripts {
  72. <style type="text/css">
  73. .row
  74. {
  75. float: left;
  76. width: 100%;
  77. padding: 10px;
  78. }
  79. .row label
  80. {
  81. width: 100px;
  82. float: left;
  83. }
  84.  
  85. #success-message
  86. {
  87. color: Green;
  88. }
  89. </style>
  90. <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
  91. <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
  92. <script type="text/javascript">
  93.  
  94. function SaveCatalogComplete(result) {
  95. $("#success-message").show();
  96. }
  97.  
  98. </script>
  99. }
  100. <h2>
  101. Item</h2>
  102. @using (Ajax.BeginForm("SaveCatalog", new AjaxOptions { HttpMethod = "POST", OnSuccess = "SaveCatalogComplete" }))
  103. {
  104.  
  105. <fieldset>
  106. <div class="row">
  107. @Html.LabelFor(x => x.CatalogName)
  108. @Html.TextBoxFor(x => x.CatalogName, Model.CatalogName)
  109. @Html.ValidationMessageFor(x => x.CatalogName)
  110. </div>
  111. <div class="row">
  112. @Html.LabelFor(x => x.Barcode)
  113. @Html.TextBoxFor(x => x.Barcode, Model.Barcode)
  114. @Html.ValidationMessageFor(x => x.Barcode)
  115. </div>
  116. </fieldset>
  117.  
  118. <div id="success-message" style="display: none;">
  119. This record is successfully saved!!
  120. </div>
  121. <div>
  122. <input type="submit" value="Save" />
  123. </div>
  124. }

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的更多相关文章

  1. MVC学习系列13--验证系列之Remote Validation

    大多数的开发者,可能会遇到这样的情况:当我们在创建用户之前,有必要去检查是否数据库中已经存在相同名字的用户.换句话说就是,我们要确保程序中,只有一个唯一的用户名,不能有重复的.相信大多数人都有不同的解 ...

  2. 转载:Remote Validation

    http://www.jb51.net/article/89474.htm 大多数的开发者,可能会遇到这样的情况:当我们在创建用户之前,有必要去检查是否数据库中已经存在相同名字的用户.换句话说就是,我 ...

  3. asp net core Remote Validation 无法验证

    [注意这里,Remote Validation是需要引入Jquery插件和启用客户端验证的]

  4. Model Validation in Asp.net MVC

    原文:Model Validation in Asp.net MVC 本文用于记录Pro ASP.NET MVC 3 Framework中阐述的数据验证的方式. 先说服务器端的吧.最简单的一种方式自然 ...

  5. [引]ASP.NET MVC 4 Content Map

    本文转自:http://msdn.microsoft.com/en-us/library/gg416514(v=vs.108).aspx The Model-View-Controller (MVC) ...

  6. MVC学习系列4--@helper辅助方法和用户自定义HTML方法

    在HTML Helper,帮助类的帮助下,我们可以动态的创建HTML控件.HTML帮助类是在视图中,用来呈现HTML内容的.HTML帮助类是一个方法,它返回的是string类型的值. HTML帮助类, ...

  7. Asp.net MVC 版本简史

    http://www.dotnet-tricks.com/Tutorial/mvc/XWX7210713-A-brief-history-of-Asp.Net-MVC-framework.html A ...

  8. Fluent Validation + NInject3 + MVC5

    Fluent Validation + NInject + MVC - Why & How : Part 1 http://fluentvalidation.codeplex.com/ htt ...

  9. Asp.net mvc 知多少(一)

    本系列主要翻译自<ASP.NET MVC Interview Questions and Answers >- By Shailendra Chauhan,想看英文原版的可访问http:/ ...

随机推荐

  1. MySql的安装与使用

    今天因为毕业设计要用到MySql数据库,所以就准备自己安装一个MySQL数据库,但是因为MySQL Install MSI只有32位,所以最后选择使用Windows (x86, 64-bit), ZI ...

  2. linux svn authorization failed错误

    authorization failed错误主要是conf/auth文件配置错误,可以参考如下配置: [aliases] # joe = /C=XZ/ST=Dessert/L=Snake City/O ...

  3. socket 基础学习

    这个示例程序是同步套接字程序,功能很简单,只是客户端发给服务器一条信息,服务器向客户端返回一条信息:这里只是一个简单的示例,是一个最基本的socket编程流程,在接下来的文章中,会依次记录套接字的同步 ...

  4. python BeautifulSoup find 方法

    这里我们重点讲一下find的几种用法,其他的类比: find(name=None, attrs={}, recursive=True, text=None, **kwargs) (ps:只讲几种用法, ...

  5. Cassandra1.2文档学习(4)——分区器

    参考文档:http://www.datastax.com/documentation/cassandra/1.2/webhelp/index.html#cassandra/architecture/a ...

  6. php获取汉字首字母的函数

    本文介绍用php实现汉字转化为首字母的方法,主要功能是:功能明确,易于修改维护和扩展: 英文的字串:不变返回(包括数字):中文字符串:返回拼音首字符: 中英混合串: 返回拼音首字符和英文. 网上的方法 ...

  7. 解决IE 下div与img重叠无法触发鼠标事件的问题

    在IE下当我想在img标签上层显示一个div元素时,此时如果该div的background为空白(没有设置图片.或者颜色填充),会导致该div的鼠标事件失效:如果设置border为1px solid ...

  8. Python环境搭建(windows)

    Python环境搭建(windows) Python简介 Python(英国发音:/ˈpaɪθən/ 美国发音:/ˈpaɪθɑːn/),是一种面向对象.直译式计算机编程语言,具有近二十年的发展历史,成 ...

  9. Python用format格式化字符串

    format是是python2.6新增的一个格式化字符串的方法,相对于老版的%格式方法,它有很多优点. 1.不需要理会数据类型的问题,在%方法中%s只能替代字符串类型 2.单个参数可以多次输出,参数顺 ...

  10. Linux命令总结(转载)

    转子:http://www.cnblogs.com/CGDeveloper/archive/2011/05/27/2060009.html 昨天看了一个教程,关于Linux命令的,本来以为当是复习随便 ...