我正在使用 Visual Studio 2012,我无法让自定义属性客户端逻辑在更小范围内重现,我创建了一个新的 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 代码?我必须在某个没有月亮的夜晚在山顶某处牺牲一些动物吗?