博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
asp.net mvc FluentValidation 的使用
阅读量:4325 次
发布时间:2019-06-06

本文共 1236 字,大约阅读时间需要 4 分钟。

  1. Create a new ASP.NET MVC 3  project using the default Visual Studio Template
  2. Download the latest
  3. Reference the FluentValidation.dll and FluentValidation.Mvc.dll assemblies (be careful there are two folders inside the .zip: MVC2 and MVC3 so make sure to pick the proper assembly)

 

新建一个Model

[Validator(typeof(MyViewModelValidator))]public class MyViewModel{	public string Title {get;set;}}

以及相应的Validator

public class MyViewModelValidator:AbstractValidator
{ public MyViewModelValidator() { RuleFor(x=>x.Title) .NotEmpty().WithMessage("pls enter title!") .Length(1,5).WithMessage("less than 5 and more than 1"); }}

在 Glabal.ass Application_Start中添加

DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;ModelValidatorProviders.Providers.Add(    new FluentValidationModelValidatorProvider(new AttributedValidatorFactory()));ModelMetadataProviders.Current = new FluentValidationModelMetadataProvider(    new AttributedValidatorFactory());

 

页面代码

@model SomeApp.Models.MyViewModel@{    ViewBag.Title = "Home Page";}@using (Html.BeginForm()){    @Html.TextBoxFor(x => x.Title)    @Html.ValidationMessageFor(x => x.Title)    }

转载于:https://www.cnblogs.com/philzhou/archive/2012/05/08/2490253.html

你可能感兴趣的文章
34 帧动画
查看>>
二次剩余及欧拉准则
查看>>
Centos 7 Mysql 最大连接数超了问题解决
查看>>
thymeleaf 自定义标签
查看>>
关于WordCount的作业
查看>>
C6748和音频ADC连接时候的TDM以及I2S格式问题
查看>>
UIView的layoutSubviews,initWithFrame,initWithCoder方法
查看>>
STM32+IAP方案 实现网络升级应用固件
查看>>
用74HC165读8个按键状态
查看>>
jpg转bmp(使用libjpeg)
查看>>
linear-gradient常用实现效果
查看>>
sql语言的一大类 DML 数据的操纵语言
查看>>
VMware黑屏解决方法
查看>>
JS中各种跳转解析
查看>>
JAVA 基础 / 第八课:面向对象 / JAVA类的方法与实例方法
查看>>
Ecust OJ
查看>>
P3384 【模板】树链剖分
查看>>
Thrift源码分析(二)-- 协议和编解码
查看>>
考勤系统之计算工作小时数
查看>>
4.1 分解条件式
查看>>