2016-04-25 48 views
3

项目:万事具备,但验证不工作

  • ASP.NET 4.5.2
  • MVC 5
  • 流利验证
  • 没有DB,没有后端

Web.config:

<appSettings> 
    <add key="webpages:Version" value="3.0.0.0" /> 
    <add key="webpages:Enabled" value="false" /> 
    <add key="ClientValidationEnabled" value="true" /> 
    <add key="UnobtrusiveJavaScriptEnabled" value="true" /> 
</appSettings> 

型号:

[Validator(typeof(ContactValidator))] 
public class ContactViewModel { 
    [DisplayName("Name:")] 
    public string Name { get; set; } 
    [DisplayName("Phone:")] 
    [DataType(DataType.PhoneNumber)] 
    public string Phone { get; set; } 
    [DisplayName("eMail:")] 
    [DataType(DataType.EmailAddress)] 
    public string eMail { get; set; } 
    [DisplayName("Message:")] 
    public string Message { get; set; } 
} 

验证:

public class ContactValidator : AbstractValidator<ContactViewModel> { 
    public ContactValidator() { 
    RuleFor(x => x.Name) 
     .NotEmpty().WithMessage("Please provide a name.") 
     .Length(2, 255).WithMessage("Please provide a name of some substantial length."); 
    RuleFor(x => x.Phone) 
     .NotEmpty().WithMessage("Please enter a valid 10-digit phone number.") 
     .Length(12, 12).WithMessage("Phone number must be in the form of &#8220;123-456-7890&#8221;") 
     .Matches(@"^\d{3}-\d{3}-\d{4}$").WithMessage("Phone number must be a valid 10-digit phone number with dashes, in the form of &#8220;123-456-7890&#8221;"); 
    RuleFor(x => x.eMail) 
     .NotNull().WithMessage("Please provide an eMail address.") 
     .EmailAddress().WithMessage("Please provide a valid eMail address to recieve the download link at."); 
    RuleFor(x => x.Message) 
     .NotNull().WithMessage("Please provide a message.") 
     .Length(2, 4000).WithMessage("Please provide a message of some substantial length."); 
    } 
} 

控制器:

[HttpGet] 
public ActionResult Contact() { 
    var model = new ContactViewModel() { }; 
    return View(model); 
} 
[HttpPost] 
[ValidateAntiForgeryToken] 
public ActionResult Contact(ContactViewModel model) { 
    if(ModelState.IsValid) { 

    return View("Index"); 
    } 
    return View(model); 
} 

查看:

@using(Html.BeginForm()) { 
    @Html.AntiForgeryToken() 
    <ul><li>All fields are required.</li></ul> 
    <fieldset> 
    @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label" })@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control", maxlength = "255" } }) 
    @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" }) 
    @Html.LabelFor(model => model.Phone, htmlAttributes: new { @class = "control-label" })@Html.EditorFor(model => model.Phone, new { htmlAttributes = new { @class = "form-control", maxlength = "12" } }) 
    @Html.ValidationMessageFor(model => model.Phone, "", new { @class = "text-danger" }) 
    @Html.LabelFor(model => model.eMail, htmlAttributes: new { @class = "control-label" })@Html.EditorFor(model => model.eMail, new { htmlAttributes = new { @class = "form-control", maxlength = "255" } }) 
    @Html.ValidationMessageFor(model => model.eMail, "", new { @class = "text-danger" }) 
    @Html.LabelFor(model => model.Message, htmlAttributes: new { @class = "control-label" })@Html.TextAreaFor(model => model.Message, new { @class = "form-control" }) 
    @Html.ValidationMessageFor(model => model.Message, "", new { @class = "text-danger" }) 
    <label class="control-label">&nbsp;</label><button type="submit" value="Save" title="Save" class="btn btn-primary glyphicon glyphicon-send"></button> 
    </fieldset> 
} 

布局视图:

<!DOCTYPE html> 
<html> 
    <head> 
    <meta charset="utf-8" /> 
    <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
    <title>Lawyer Case | @ViewBag.Title</title> 
    @Styles.Render("~/Content/bootstrap") 
    @Styles.Render("~/Content/lightbox") 
    @Styles.Render("~/Content/css") 
    @Scripts.Render("~/bundles/modernizr") 
    @Scripts.Render("~/bundles/jquery") 
    @Scripts.Render("~/bundles/jqueryval") 
    @Scripts.Render("~/bundles/bootstrap") 
    @Scripts.Render("~/bundles/inputmask") 
    @Scripts.Render("~/bundles/lightbox") 
    </head> 
    <body> 
    <div class="navbar navbar-blue navbar-fixed-top"> 
     <div class="container"> 
    <header class="navbar-header"> 
     <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"> 
     <span class="icon-bar"></span> 
     <span class="icon-bar"></span> 
     <span class="icon-bar"></span> 
     </button> 
     <h1>@Html.ActionLink("Lawyer Case", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })</h1> 
    </header> 
    <nav class="navbar-collapse collapse"> 
     <ul class="nav navbar-nav"> 
     <li>@Html.ActionLink("Home", "Index")</li> 
     <li>@Html.ActionLink("Contact Us", "Contact")</li> 
     </ul> 
    </nav> 
     </div> 
    </div> 
    <main class="container body-content"> 
     @RenderBody() 
    </main> 
    <footer class="container"> 
     <hr /> 
     <p>Site Contents &copy; @DateTime.Now.Year - Lawyer Case.<br />Site Design &amp; Development &copy; @DateTime.Now.Year - Eclipse Computing Ltd.</p> 
    </footer> 
    @RenderSection("scripts", required: false) 
    </body> 
</html> 

不要紧其中我把@Scripts.Render("~/bundles/jqueryval"),验证工作不为任何客户端或服务器端。只是有点困惑在这里...

如果我只是打“提交”表单上没有填写出来的话,那应该返回我的联系人页面和形式,因为ModelState.IsValid应该false ,但显然它不是 - 我得到的索引页面,这是告诉我,验证不正确的验证,因为表单是空的ModelState.IsValid = true。为什么,我不知道。

+0

你是否确信通过'FluentValidationModelValidatorProvider.Configure()来连线它的Application_Start();'? –

+0

Oooookay ...任何机会,你可以添加作为解决方案,所以我可以标记为正确的答案,并给你一些观点?另外请注意,它需要进入Global.asax.cs文件,因为我必须在正确的位置进行一些寻找。另外 - 当你通过NuGet带来一个像FluentValidation这样的软件包时,为什么不这样做会自动配置?这看起来像是一件容易的事情 - 如果你把软件包带进来,显然你想使用它,所以让我们自动配置项目,不管你将如何实现该工具,都需要一些基本的必需品。 –

回答

4

根据文档here

您将需要配置与MVC集成 - 通常是在你的Global.asax.cs

FluentValidationModelValidatorProvider.Configure();