【发布时间】:2016-08-07 18:27:57
【问题描述】:
我正在尝试创建客户端验证。当我将页面上的字段保持为空并单击提交按钮时,服务器端验证正在工作,但客户端验证不起作用。我还在 layout.cshtml 文件中包含jqueryval 捆绑包。
@Scripts.Render("~/bundles/jqueryval")
我还检查了 web.config 文件中的以下行
<add key="ClientValidationEnabled" value="true" />
型号:
public class File
{
[Required]
public string Name { get; set; }
[Required]
public string Description { get; set; }
public DateTime DateCreated { get; set; }
public DateTime DateModified { get; set; }
[Required]
public string FileSrc { get; set; }
}
查看:
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>File</h4>
<hr />
@Html.ValidationSummary(false, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextAreaFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.FileSrc, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<input type="file" id="FU" multiple />
@Html.HiddenFor(model => model.FileSrc,new { id="fm"})
@Html.ValidationMessageFor(model => model.FileSrc, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
我有什么遗漏吗?
【问题讨论】:
-
显示你的一些模型和视图(你是否也包括了
jquery-{version}.js) -
是的
jquery-{version}.js 包括 -
@StephenMuecke _Layout.cshtml 还是实际视图?
-
视图(只是模型的 1 或 2 个属性,包括其验证属性以及如何为它们生成视图)
-
视图和模型看起来很好(除了
DateCreated上的[Required]不是真正需要的 -DataTime不能是null和@Html.ValidationMessageFor(model => model.FileSrc不会工作,因为相关的输入被隐藏)。您是否在浏览器控制台中遇到任何错误?
标签: asp.net-mvc-5