1, Fluentvalidation Introduction
Fluentvalidation is a data validation component that differs from the ASP. NET Dataannotataion attribute authentication entity, providing a way to validate the separation of entities from validation. The Fluentvalidation also provides a chain-of-expression syntax.
2, Installation Fluentvalidation
Fluentvalidation Address: http://fluentvalidation.codeplex.com/
Install Fluentvalidation and FLUENTVALIDATION.MVC using the management NuGet package for Visual Studio
3. Using fluentvalidation verification through modelstate
Project Solution Structure diagram:
Entity class Customer.cs:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;namespacelibing.portal.web.models.entities{ Public classCustomer { Public intCustomerID {Get;Set; } Public stringCustomerName {Get;Set; } Public stringEmail {Get;Set; } Public stringAddress {Get;Set; } Public stringPostcode {Get;Set; } Public float? Discount {Get;Set; } Public BOOLHasdiscount {Get;Set; } }}
Data validation class CustomerValidator.cs:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingfluentvalidation;usingLibing.Portal.Web.Models.Entities;namespacelibing.portal.web.models.validators{ Public classCustomervalidator:abstractvalidator<customer> { PublicCustomervalidator () {rulefor (Customer= Customer. CustomerName). Notnull (). Withmessage ("the customer name cannot be empty"); Rulefor (Customer=customer. Email). Notempty (). Withmessage ("The mailbox cannot be empty") . EmailAddress (). Withmessage ("The mailbox format is not correct"); Rulefor (Customer=customer. Discount). NotEqual (0) . When (Customer=customer. Hasdiscount); Rulefor (Customer=customer. Address). Notempty (). Withmessage ("address cannot be empty") . Length ( -, -) . Withmessage ("address length range is 20-50 bytes"); } }}
Controller class CustomerController.cs:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSYSTEM.WEB.MVC;usingFluentvalidation.results;usingLibing.Portal.Web.Models.Entities;usingLibing.Portal.Web.Models.Validators;namespacelibing.portal.web.controllers{ Public classCustomercontroller:controller { PublicActionResult Index () {returnView (); } PublicActionResult Create () {returnView (); } [HttpPost] PublicActionResult Create (Customer customer) {Customervalidator Validator=NewCustomervalidator (); Validationresult result=Validator. Validate (customer); if(!result. IsValid) {result. Errors.tolist (). ForEach (Error={modelstate.addmodelerror (error. PropertyName, error. ErrorMessage); }); } if(modelstate.isvalid) {returnRedirecttoaction ("Index"); } returnView (customer); } }}
View Page create.cshtml, which is automatically generated when you add a view by selecting the Create Template:
@model libing.portal.web.models.entities.customer@{Layout = null;}<!DOCTYPE HTML><HTML><Head> <Metaname= "Viewport"content= "Width=device-width" /> <title>Create</title></Head><Body>@using (Html.BeginForm ()) {@Html. AntiForgeryToken ()<Divclass= "Form-horizontal"> <h4>Customer</h4> <HR/>@Html. ValidationSummary (True)<Divclass= "Form-group">@Html. labelfor (model = model. CustomerName, new {@class = "Control-label col-md-2"})<Divclass= "Col-md-10">@Html. editorfor (model = model. CustomerName) @Html. validationmessagefor (model = model. CustomerName)</Div> </Div> <Divclass= "Form-group">@Html. labelfor (model = model. Email, new {@class = "Control-label col-md-2"})<Divclass= "Col-md-10">@Html. editorfor (model = model. Email) @Html. validationmessagefor (model = model. Email)</Div> </Div> <Divclass= "Form-group">@Html. labelfor (model = model. Address, new {@class = "Control-label col-md-2"})<Divclass= "Col-md-10">@Html. editorfor (model = model. Address) @Html. validationmessagefor (model = model. Address)</Div> </Div> <Divclass= "Form-group">@Html. labelfor (model = model. Postcode, new {@class = "Control-label col-md-2"})<Divclass= "Col-md-10">@Html. editorfor (model = model. Postcode) @Html. VALIDATIONMESSAGEFOR (model = model. Postcode)</Div> </Div> <Divclass= "Form-group">@Html. labelfor (model = model. Discount, new {@class = "Control-label col-md-2"})<Divclass= "Col-md-10">@Html. editorfor (model = model. Discount) @Html. validationmessagefor (model = model. Discount)</Div> </Div> <Divclass= "Form-group">@Html. labelfor (model = model. Hasdiscount, new {@class = "Control-label col-md-2"})<Divclass= "Col-md-10">@Html. editorfor (model = model. Hasdiscount) @Html. validationmessagefor (model = model. Hasdiscount)</Div> </Div> <Divclass= "Form-group"> <Divclass= "Col-md-offset-2 col-md-10"> <inputtype= "Submit"value= "Create"class= "Btn Btn-default" /> </Div> </Div> </Div> }</Body></HTML>
Operating effect: