这真的是不显眼的 jquery 验证吗?
根据此处的定义:
Unobtrusive JavaScript 避免将内联 JavaScript 注入 HTML。这使得您的 HTML 更小、更简洁,并且可以更轻松地替换或自定义 JavaScript 库。
但是,当阅读此博客: 博主说: 现在,为了急切地执行验证,即每次用户字段聚焦时执行验证,您需要在页面底部添加此脚本。
我不知道我是否感到困惑,但对我来说,简单的 JavaScript 验证意味着页面上没有 JavaScript 代码,只有对具有逻辑的 .js 文件的引用。通过这种方式,您可以将验证逻辑与表示分离。
话虽如此,我想在下面的表单中启用不显眼的验证。我看到的唯一区别是: 1. 第 1 行:我没有。
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<EagerlyPerformingValidation.Models.UserInformation>
- 我应该将 jquery 逻辑添加到外部 javascript 文件并引用它。
我在这里错过了什么吗?
public class Position{
[DatabaseGenerated(System.ComponentModel.DataAnnotations.DatabaseGeneratedOption.Identity)]
public int PositionID { get; set; }
[Required(ErrorMessage = "Position name is required.")]
[StringLength(20, MinimumLength = 3, ErrorMessage = "Name should not be longer than 20 characters.")]
[Display(Name = "Position name")]
public string name { get; set; }
[Required(ErrorMessage = "Number of years is required")]
[Display(Name = "Number of years")]
[YearsValidationAttribute(5, ErrorMessage = "{0} value must be greater than {1} years.")]
public int yearsExperienceRequired { get; set; }
public virtual ICollection<ApplicantPosition> applicantPosition { get; set; }
}
@model Data.Model.Position
@{
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Position</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.yearsExperienceRequired)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.yearsExperienceRequired)
@Html.ValidationMessageFor(model => model.yearsExperienceRequired)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据您的(正确)理解完成的不引人注目的验证有点“懒惰”,因为它仅在您提交表单时才会触发。您链接到的博客文章引用的 javascript 代码只会导致每次您跳出控件时触发验证。你是对的,它并不是真正不引人注目的 JavaScript。
我认为使它不引人注目的方法是向每个控件添加
data-
属性(很痛苦)并编写代码来注入脚本,或者将他的一点脚本添加到其中一个您正在引用的主要 javascript 文件,这可能是他的意图。本书第 8 章对此有一个很酷的内容:Wrox 专业 ASP.NET MVC 3
The unobtrusive validation done according to your (correct) understanding is kind of "lazy" in that it only fires when you submit the form. The javascript code referenced by the blog post you linked to simply causes the validation to fire each time you tab out of a control. You're right that it's not really unobtrusive javascript as presented.
I think the way you could make it be unobtrusive is either to add
data-
attributes to each control (a pain) and write code to inject the script, or to add his little bit of script to one of the main javascript files you are referencing, which is probably his intention.There is a cool bit about this in chapter 8 of this book: Wrox Professional ASP.NET MVC 3