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. android开发之GenyMotion与intelliJ的配置

    (注意:这是在你的电脑上安装了intelliJ和安卓SDK后才进行的工作,如果没有intelliJ和安卓SDK,请先安装以上两样东西) 号称史上最快乐的模拟器GenyMotion,试一下. 第一步:下 ...

  2. OpenJudge/Poj 2013 Symmetric Order

    1.链接地址: http://bailian.openjudge.cn/practice/2013 http://poj.org/problem?id=2013 2.题目: Symmetric Ord ...

  3. CentOS7 安装LAMP环境

    1.使用yum安装 yum -y install httpd mysql mysql-server php php-mysql postgresql postgresql-server php-pos ...

  4. tomcat错误信息解决方案 严重:StandardServer.await:

    看到这个报错我的第一反应就是端口被占用,用netstat -ant命令查看发现8080端口没有被占用,也可以看到 tomcat的进程已经存在,但是不能对外提供服务. 1.独立运行的tomcat.exe ...

  5. ECMAScript 6十大特性

    ES6入门 http://es6.ruanyifeng.com/ ES6排名前十的最佳特性列表 Default Parameters(默认参数) in ES6 Template Literals (模 ...

  6. struts2 修改action的后缀

    struts2 修改action的后缀 struts2 的默认后缀是 .action 虽然很直观,但是很烦琐.很多人喜欢将请求的后缀改为 .do 在struts2中修改action后缀有两种比较简单的 ...

  7. python 调用第三方库压缩png或者转换成webp

    因为工作需要去研究了下png的压缩,发现转换成webp可以小很多,但是webp在手机上的解码速度比png的解码速度慢很多.出于进几年手机设备的处理器的性能也不错了,所以准备两套方案. 在网上搜索了一些 ...

  8. python 中函数的参数

    一.python中的函数参数形式 python中函数一般有四种表现形式: 1.def function(arg1, arg2, arg3...) 这种是python中最常见的一中函数参数定义形式,函数 ...

  9. c语言贪吃蛇

    思路:函数gotoxy(x,y)使光标移植屏幕的x,y坐标(屏幕左上角为0,0),用来绘制蛇和界面,color()函数用来设置绘制的颜色.设有snakelong节,第i节蛇的x坐标为x[i],y坐标为 ...

  10. stdout 编码 vim 删除左边,右边

    sys.stdout = codecs.getwriter('utf8')(sys.stdout) vimdic['kkkk'] = qqqqqdic['bbbb'] = aaaaaadic['kkk ...