FluentValidation验证
参考:http://www.c-sharpcorner.com/UploadFile/3d39b4/Asp-Net-mvc-validation-using-fluent-validation/
创建一个Customer类
public class Customer
{
public string Name { get; set; }
public string Email { get; set; }
}
引用FluentValidation.dll
创建CustomerValidator
public class CustomerValidator : AbstractValidator<Customer>
{
public CustomerValidator()
{
RuleFor(x => x.Name).NotEmpty().WithMessage("Name is required");
RuleFor(x => x.Email).NotEmpty().WithMessage("Email is required");
RuleFor(x => x.Email).EmailAddress().WithMessage("Email is not valid");
}
}
创建Customer控制器
public class CustomerController : Controller
{
//
// GET: /Customer/ public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(Customer model)
{
CustomerValidator validator = new CustomerValidator();
ValidationResult result = validator.Validate(model);
if (result.IsValid)
{
ViewBag.Name = model.Name;
ViewBag.Email = model.Email;
}
else
{
foreach (ValidationFailure failer in result.Errors)
{
ModelState.AddModelError(failer.PropertyName, failer.ErrorMessage);
}
}
return View(model);
}
}
View:
@model ValidationAndMVC.Models.Customer
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@if (ViewData.ModelState.IsValid)
{
<b>
Name : @ViewBag.Name<br />
Email : @ViewBag.Email
</b>
}
@using (Html.BeginForm())
{
<fieldset>
<legend>Customer</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Email)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Email)
@Html.ValidationMessageFor(model => model.Email)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
<h2>Index</h2>
效果

FluentValidation验证的更多相关文章
- .NET平台开源项目速览(10)FluentValidation验证组件深入使用(二)
在上一篇文章:.NET平台开源项目速览(6)FluentValidation验证组件介绍与入门(一) 中,给大家初步介绍了一下FluentValidation验证组件的使用情况.文章从构建间的验证器开 ...
- .NET平台开源项目速览(6)FluentValidation验证组件介绍与入门(一)
在文章:这些.NET开源项目你知道吗?让.NET开源来得更加猛烈些吧!(第二辑)中,给大家初步介绍了一下FluentValidation验证组件.那里只是概述了一下,并没有对其使用和强大功能做深入研究 ...
- ASP.NET MVC中使用FluentValidation验证实体
1.FluentValidation介绍 FluentValidation是与ASP.NET DataAnnotataion Attribute验证实体不同的数据验证组件,提供了将实体与验证分离开来的 ...
- ASP.NET MVC中使用FluentValidation验证实体(转载)
1.FluentValidation介绍 FluentValidation是与ASP.NET DataAnnotataion Attribute验证实体不同的数据验证组件,提供了将实体与验证分离开来的 ...
- NET平台开源项目速览(6)FluentValidation验证组件介绍与入门(转载)
原文地址:http://www.cnblogs.com/asxinyu/p/dotnet_Opensource_project_FluentValidation_1.html 阅读目录 1.基本介绍 ...
- Rookey.Frame之实体FluentValidation验证
昨天给大家介绍了Rookey.Frame框架的实体设计,今天继续跟大家分享实体的FluentValidation验证,在Rookey.Frame框架中可以设置多种验证方式:FluentValidati ...
- ASP.NET Core WebApi中使用FluentValidation验证数据模型
原文链接:Common features in ASP.NET Core 2.1 WebApi: Validation 作者:Anthony Giretti 译者:Lamond Lu 介绍 验证用户输 ...
- 【翻译】FluentValidation验证组件的使用
由于本文是翻译,所以将原文原原本本的搬上来,大家看原文有什么不懂的也可以对照这里. 给出地址:https://fluentvalidation.net/ FluentValidation fluent ...
- .Net Core3.0 WEB API 中使用FluentValidation验证,实现批量注入
为什么要使用FluentValidation 1.在日常的开发中,需要验证参数的合理性,不紧前端需要验证传毒的参数,后端也需要验证参数 2.在领域模型中也应该验证,做好防御性的编程是一种好的习惯(其实 ...
- 关于简单的 FluentValidation 验证
FluentValidation : https://github.com/JeremySkinner/FluentValidation 关于为何要使用,因为微软自带的模型验证有点弱,还需要自己去写 ...
随机推荐
- template.js遍历对象的写法
<!DOCTYPE html><html><head> <meta charset="utf-8"> <title>菜鸟 ...
- ajax 对象创建 兼容各个浏览器
<script> function createAjax(){ var request=false; //window对象中有XMLHttpRequest存在就是非IE,包括(IE7,IE ...
- An exception occurred while acquiring a poolable resource. Will retry.
我的是mysql-connector-java-5.1.20.jar加到lib下面就行了.或者在pom中加入配置也行.
- Xcode7中你一定要知道的炸裂调试神技
转自:http://www.cocoachina.com/ios/20151020/13794.html Xcode7中苹果为我们增加了两个重要的debug相关功能.了解之后觉得非常实用,介绍给大家. ...
- spring注解记录
集中记录spring常见注解 供今后查阅 @ControllerAdvice: ControllerAdvice的定义为: @Target(ElementType.TYPE) @Retention(R ...
- python day 1--python初始
笔者:QQ: 360212316 Python初识 Python的创始人为Guido van Rossum.1989年圣诞节期间,在阿姆斯特丹,Guido为了打发圣诞节的无趣,决心开发一个新的脚本 ...
- EXCEL数据导入dataset
一.开工必备 1.micorosoft office2007 2.VS2010.Oracle 11 二.界面 三.内部代码 (1)获取数据库连接,定义全局变量 private static strin ...
- 将复杂查询写到SQL配置文件--SOD框架的SQL-MAP技术简介
引言 今天看到一片热门的博客, .NET高级工程师面试题之SQL篇 ,要求找出每一个系的最高分,并且按系编号,学生编号升序排列.这个查询比较复杂,也比较典型,自从用了ORM后,很久没有写过SQL语句了 ...
- C6713的Boot mode
2014年7月23日,终于把困扰我两个月的问题解决了,甚是嗨皮,所以做下记录,以供后人参考之用. 问题描述:我用的片子是TMS320C6713,通过EMIF总线连接的FLASH,此FLASH分为两部分 ...
- windbg命令----!idt
!idt扩展显示指定的中断分配表(interrupt dispatch table (IDT))中的中断服务例程(interrupt service routine (ISR)) -a 没有指定IDT ...