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.
Copy Codeusing 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:/ ...
随机推荐
- PHP - 5.4 Array dereferencing 数组值
在5.4之前我们直接获取数组的值得方法如下 <?php $str = 'a;b;c;d'; list($value) = explode(';',$str); echo $value; 结果为: ...
- OS X平台上MySQL环境搭建
参考资料: http://www.cnblogs.com/macro-cheng/archive/2011/10/25/mysql-001.html http://blog.csdn.net/just ...
- Web前端新人之CSS样式选择器
最近在学习css样式.那么我就想先整理一下css样式的选择器 规则结构: 每个规则都有两个基本部分:选择器和声明块.声明块由一个或者多个声明组成,每个声明则是一个属性—值对(property-valu ...
- htmlt中的块状元素与内联元素
块元素(block element) address - 地址 blockquote - 块引用 center - 举中对齐块 dir - 目录列表 div - 常用块级容易,也是CSS layout ...
- 使用webstorm上传代码到github
使用webstorm上传代码到github 字数681 阅读330 评论0 喜欢5 之前使用过webstorm上传代码到github,过了几个月竟然发现自己忘记了,好记性不如烂笔头啊,今天又重新用了一 ...
- 之前可运行mongodb,后来却不行了显示Unclean shutdown detected mongodb
解决办法有三个: 第一个:如果你之前可以运行,说明你已经有数据存放目录了,你可以把数据存放目录之前的数据清空再启动,在配置一下 第二个:使用mongod --repair --dbpath D:\Mo ...
- DBA
一个公司的数据库系统也是由DBA 来进行管理的,它们的主要工作如下: l 安装和配置数据库,创建数据库以及帐户:l 监视数据库系统,保证数据库不宕机:l 收集系统统计和性能信息以便进行调整:l 发现性 ...
- Ubuntu下安装php调试工具xdebug
安装xdebug: sudo apt-get install php-pear sudo apt-get install php5-dev pecl install xdebug 创建配置文件: /x ...
- 【4】创建一个自己的Bootstrap模板
什么也不说了,直接贴上代码吧,哈哈 <!DOCTYPE html> <html lang="zh-cn"> <head> <meta ch ...
- Python设计模式——外观模式
外观模式跟代理模式有点像,都是在client和目标的类之间建一个中间的类,client不直接调用目标的类,而是通过先调用中间类的方法,由中间类来实现怎么调用目标类. 代理模式用这种模式的目的是可以实现 ...