【发布时间】:2017-12-19 22:26:18
【问题描述】:
我有一个使用实体框架 SQL Server 模型的 MVC 5 应用程序。我使用 Visual Studio 中的模板构建它。
页面运行良好,但日期验证让我很闲。
验证只会让我添加一个向后的日期,例如yyyy/mm/dd 或美国。我已将英国文化添加到 webconfig,但这并没有帮助。有没有办法用 uk 格式覆盖 jquery,我会将它放在我的代码中的什么位置?
抱歉,我对 asp mvc 和 razor 完全陌生。
我的代码如下:
@model messageBoard.Models.tblMessage
@{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>tblMessage</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.MessageID)
<div class="form-group">
@Html.LabelFor(model => model.Sender, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Sender, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Sender, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Receiver, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Receiver, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Receiver, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.YearGroup, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.YearGroup, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.YearGroup, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Date, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Date, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Date, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Expiry, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Expiry, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Expiry, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Message, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Message, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Message, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
【问题讨论】:
-
您需要将服务器上的文化更改为接受
dd/MM/yyyy格式的日期(例如在web.config文件中) -
“我知道 sql server 以这种格式存储日期”。不,它没有,它使用与任何人类可读格式无关的内部表示。此外,验证规则在 MVC 中,与 SQL 无关。
-
我同意这与 MVC 验证有关,与 SQL 无关。无论如何,您能否扩展“验证只会让我添加日期”-您在做什么以及发生了什么?(您看到什么消息)
-
这个可怜的问题是我的错,对不起,我是 MVC 的新手。基本上我相信它是 jquery 验证,它阻止了我以英国格式 dd/mm/yyyy 输入日期。但是我不知道如何改变这一点。我将此行添加到 webconfig
。然而它并没有帮助。还有其他想法吗? -
默认情况下,jquery.validate.js`基于
MM/dd/yyyy格式进行验证。如果您想以dd/MM/yyyy格式输入日期并且(比如)30/12/2017 作为验证日期,那么您需要重新配置验证器。
标签: jquery asp.net-mvc date