我使用Visual Studio 2012,我无法获得客户端逻辑的自定义属性
为了更小规模地再现,我创建了一个新的MVC 4项目,我创建了以下模型,并且将永远不会验证的属性
为了更小规模地再现,我创建了一个新的MVC 4项目,我创建了以下模型,并且将永远不会验证的属性
public class MyModel { public int Id { get; set; } [required] public string LastName { get; set; } [NeverValid(ErrorMessage="Serverside Will Never Validate")] public string FirstName { get; set; } } public class NeverValidAttribute : ValidationAttribute,IClientValidatable { public override bool IsValid(object value) { return false; } protected override ValidationResult IsValid(object value,ValidationContext validationContext) { return new ValidationResult(this.ErrorMessage,new[] { validationContext.MemberName }); } public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata Metadata,ControllerContext context) { yield return new ModelClientValidationRule { ErrorMessage = this.ErrorMessage,ValidationType = "nevervalid" }; } }
然后我将以下操作添加到HomeController中
public ActionResult Index() { return View(new MyModel()); } [HttpPost] public ActionResult Index(MyModel model) { if (!ModelState.IsValid) { // Will Always Be Invalid } return View(model); }
还有一个名为nevervalid.js的javascript文件
$(function () { $.validator.addMethod("nevervalid",function () { return false; },"Clientside Should Not Postback"); $.validator.unobtrusive.adapters.addBool("nevervalid"); });
和索引视图
@model CustomAttribute.Models.MyModel @{ ViewBag.Title = "Home Page"; } @using (Html.BeginForm()) { @Html.ValidationSummary(true) <fieldset> <legend>MyModel</legend> @Html.HiddenFor(model => model.Id) <div class="editor-label"> @Html.LabelFor(model => model.LastName) </div> <div class="editor-field"> @Html.EditorFor(model => model.LastName) @Html.ValidationMessageFor(model => model.LastName) </div> <div class="editor-label"> @Html.LabelFor(model => model.FirstName) </div> <div class="editor-field"> @Html.EditorFor(model => model.FirstName) @Html.ValidationMessageFor(model => model.FirstName) </div> <p> <input type="submit" value="Save" /> </p> </fieldset> } @section Scripts { @Scripts.Render("~/bundles/jqueryval") @Scripts.Render("~/Scripts/nevervalid.js") }
我的web.config中的相关区域看起来像这样
<appSettings> <add key="webpages:Version" value="2.0.0.0" /> <add key="webpages:Enabled" value="false" /> <add key="PreserveLoginUrl" value="true" /> <add key="ClientValidationEnabled" value="true" /> <add key="UnobtrusiveJavaScriptEnabled" value="true" /> </appSettings>
当页面加载时,将加载以下文件(从Chrome的F12上的网络选项卡获取)
http://localhost:7440/ http://localhost:7440/Content/site.css http://localhost:7440/Scripts/modernizr-2.5.3.js http://localhost:7440/Scripts/jquery-1.7.1.js http://localhost:7440/Scripts/jquery.unobtrusive-ajax.js http://localhost:7440/Scripts/jquery.validate.js http://localhost:7440/Scripts/jquery.validate.unobtrusive.js http://localhost:7440/Scripts/nevervalid.js
<input class="text-Box single-line valid" data-val="true" data-val-nevervalid="Serverside Will Never Validate" id="FirstName" name="FirstName" type="text" value="">
所以,我问你为什么哦,为什么这个东西必须回发做服务器端验证,而我有一些完美的JavaScript代码在这里?在某处山顶上的月亮夜晚,我要牺牲一些动物吗?
解决方法
我想知道如果您更改了nevervalid.js,以便在文档准备好之前执行代码,会发生什么.
我的基本思想是,当dom准备好时,通过解析文档来进行不引人注目的验证,但是如果您在同一时间/之后加载自定义验证的东西,那么它不会知道当它到达时该怎么做您自定义验证数据属性.
无论如何,尝试改变nevervalid.js只是:
(function($) { $.validator.addMethod("nevervalid","Clientside Should Not Postback"); $.validator.unobtrusive.adapters.addBool("nevervalid"); })(jQuery);