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

  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. 国内IT技术博客对比

    今天我想就自己对用了国内几个IT行业领先的博客做一个心得体会的总结: 我总共是用了三个,第一个是新浪,第二个是CSDN,第三个是博客园: 当然期间有自己搭建过wordpress,也用了一段时间,但是感 ...

  2. C++学习 (转)

    1.把C++当成一门新的语言学习: 2.看<Thinking In C++>: 3.看<The C++ Programming Language>和<Inside The ...

  3. php中位运算的应用:货品的状态

    效果如下图: 分析:用一个整数的二进制可以记录32状态 00000000 00000000 00000000 00000000  >>=0 从右往左保存这三个的状态: 精品选中,第一位设置 ...

  4. MS SQL 维护小记

    --查看当前连接的会话信息(进程号1--50是SQL Server系统内部用的) SELECT * FROM sys.dm_exec_sessions WHERE session_id >=51 ...

  5. iis7.5 aspx,ashx的mime类型

    映射aspx: 打开IIS管理器,找到“处理程序映射”,在列表右击选择“添加脚本映射”即可.eg:*.aspx,将该类型的页面的处理程序映射为“%windir%\Microsoft.NET\Frame ...

  6. 写 一个PHP脚本遇到的问题总结

    在项目中,因为之前的人员,基础数据没有处理好,后面需要写一个脚本来处理这个问题,经验少,总结如下: 1.在linux下直接连接跑处理MySQL数据的脚本,要用PDO的方式连接数据库,长时间在框架中处理 ...

  7. linux shell命令的常用快捷键

    一些shell的常用快捷键.   Ctrl + a 切换到命令行开始  Ctrl + e 切换到命令行末尾  Ctrl + l 清除屏幕内容  Ctrl + u 清除剪切光标之前的内容  Ctrl + ...

  8. Reactor模式(反应器模式)

    Reactor这个词译成汉语还真没有什么合适的,很多地方叫反应器模式,但更多好像就直接叫reactor模式了,其实我觉着叫应答者模式更好理解一些.通过了解,这个模式更像一个侍卫,一直在等待你的召唤,或 ...

  9. platform平台设备驱动简化示例代码

    driver.c: #include <linux/kernel.h> #include <linux/module.h> #include <linux/init.h& ...

  10. ibatis访问oracle数据库US7ASCII中文乱码问题

    今天碰到一个问题,使用ibatis框架访问编码为US7ASCII的oracle数据中文乱码, 找了很久终于有了解决方案 首先 SqlMap-Config.xml按如下配置 <sqlMapConf ...