zoukankan      html  css  js  c++  java
  • 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>

    效果

  • 相关阅读:
    [SDOI2009]生日礼物(单调队列)
    [luogu1638]逛画展(单调队列)
    【最短路】·SPFA算法实现
    [UVA10474]大理石在哪儿
    【转载】C++中string erase函数的使用
    【转载】高精度减法的OP写法
    【转载】ST表
    串门赛: NOIP2016模拟赛——By Marvolo 丢脸记
    Bzoj 3813 奇数国 题解 数论+线段树+状压
    Bzoj 2064 分裂 题解
  • 原文地址:https://www.cnblogs.com/dalovess/p/5606279.html
Copyright © 2011-2022 走看看