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. 【nodemailer】 之邮件附件

    nodemailer 续 之前对nodemailer做了一个简单的了解,这篇文章主要研究一下如何添加附加文件 测试代码 //Created by yyrdl on 2015/10/2. var nod ...

  2. MySQL中,修改表的某一字段的部分值

    语法:update 表名 set 字段名 = replace(字段名,'替换前内容','替换后的内容') where 条件. 如: 执行sql语句:update student set name = ...

  3. 小shell函数

     whoport() {  port=$1  echo "------ who occupied port: $port ----------"  info=$(sudo lsof ...

  4. 链表C++模板实现

    #include <iostream.h> #include <stdlib.h> //结点模板类 template <typename t1, typename t2& ...

  5. JS判断是否微信浏览器

    JS判断是否微信浏览器 function isWeixinBrowser(){ var ua = navigator.userAgent.toLowerCase(); return (/microme ...

  6. Linux oracle数据库自动备份自动压缩脚本代码

    Linux oracle数据库备份完成后可以自动压缩脚本代码. 复制代码代码如下: #!/bin/bash #backup.sh #edit: www.jbxue.com ##系统名称 sysname ...

  7. Oracle控制文件丢失,日志文件丢失

    控制文件丢失: alter database backup controlfile to traces; shutdown immediate; @j:\db\script\orcl_ora_ctl_ ...

  8. windows下STM32开发环境的搭建

    一.概述 1.说明 笔者已经写了一篇Linux下STM32开发环境的搭建 ,这两篇文章的最区别在于开发环境所处的系统平台不一样,而其实这个区别对于开发环境的搭建其实影响不大,制作局部上的操作上发生了改 ...

  9. 设置Tomcat应用自动部署目录

    只需要在Tomcat/conf目录下面新建文件夹Catalina/localhost,然后再localhost文件夹下面新建一个[应用名字.xml]文件即可,有多少个应用就新建多少个xml文件即可,x ...

  10. vs2013 上传碰到的问题:“输入的不是有效的 Base-64 字符串 ”

    action 代码: [HttpPost] [ValidateAntiForgeryToken] public ActionResult Create( ImageStoreModels images ...