- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
项目:
网络配置:
<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 “123-456-7890”")
.Matches(@"^\d{3}-\d{3}-\d{4}$").WithMessage("Phone number must be a valid 10-digit phone number with dashes, in the form of “123-456-7890”");
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.");
}
}
Controller :
[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"> </label><button type="submit" value="Save" title="Save" class="btn btn-primary glyphicon glyphicon-send"></button>
</fieldset>
}
布局 View :
<!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 © @DateTime.Now.Year - Lawyer Case.<br />Site Design & Development © @DateTime.Now.Year - Eclipse Computing Ltd.</p>
</footer>
@RenderSection("scripts", required: false)
</body>
</html>
我把 @Scripts.Render("~/bundles/jqueryval")
放在哪里没关系,验证对客户端都不起作用 < strong>OR 服务器端。只是有点迷惑...
如果我只是点击表单上的“提交”而根本没有填写它,它应该让我返回到联系页面和表单,因为 ModelState.IsValid
应该 false
,但显然不是——我得到的是索引页,它告诉我验证不是正确验证,因为表单为空 ModelState.IsValid = true
。为什么,我不知道。
最佳答案
根据文档 here .
您需要配置与 MVC 的集成 - 通常在 Global.asax.cs
Application_Start()
中
FluentValidationModelValidatorProvider.Configure();
关于asp.net - 一切就绪,但验证不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36850041/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!