[英]Validation failing because of date format
I have an MVC6 Edit View with a date of birth field as follows: 我有一个带有出生日期字段的MVC6编辑视图,如下所示:
<div class="form-group">
@Html.LabelFor(model => model.DateOfBirth, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@*@Html.EditorFor(model => model.DateOfBirth, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.DateOfBirth, "", new { @class = "text-danger" })*@
@Html.EditorFor(m => m.DateOfBirth,new { htmlAttributes = new { @readonly = "readonly" } })
@Html.ValidationMessageFor(model => model.DateOfBirth, "", new { @class = "text-danger" })
</div>
</div>
Therefore when I click save it errors saying: The value '07/17/1981' is not valid for ApplicationDate. 因此,当我单击“保存”时,错误提示:值'07 / 17/1981'对ApplicationDate无效。 How can I ensure that the date is displayed in UK format meaning validation succeeds. 如何确保日期以英国格式显示,意味着验证成功。
I have added the following to the Bubdle.Config following an answer below: 我将以下答案添加到Bubdle.Config中:
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery-{version}.js",
"~/Scripts/globalize.js",
"~/Scripts/jquery.validate.globalize.js"));
Update your model like this: 像这样更新模型:
// Add this attribute
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public DateTime DateOfBirth { get; set; }
I'm assuming your error is client-side? 我假设您的错误是客户端? ie Unobtrusive validation? 即不打扰的验证? An alternative solution to the one already posted is to use the Globalization. 已发布的解决方案的另一种解决方案是使用全球化。
There is a Nuget package available "jQuery.Validation.Globalize. You can install this and simply include the globalize Javascript files with your jqueryval bundles. 有一个可用的Nuget包“ jQuery.Validation.Globalize。您可以安装此包,并在jqueryval捆绑包中包含全球化的Javascript文件。
You can do this by navigation to the App_Start
directory and editing the BundleConfig.cs
file. 您可以通过导航到App_Start
目录并编辑BundleConfig.cs
文件来实现。 You'll see there should already be a ScriptBundle
for bundles/jqueryval
- You can add the path to the globalize.js
and jquery.val.globalize.js
files to that, or create a separate ScriptBundle
to reference in your View. 您将看到应该已经有用于bundles/jqueryval
的ScriptBundle
您可以将globalize.js
和jquery.val.globalize.js
文件的路径添加到其中,或者创建单独的ScriptBundle
以在View中引用。
Note: This will only work if you add the CultureInfo
to your Global.asax file as below. 注意:仅当按如下所示将CultureInfo
添加到Global.asax文件时,此方法才有效。
You'll also need to add the below to your Global.asax file. 您还需要将以下内容添加到Global.asax文件中。
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB");
In the director Views/Shared/EditorTemplates
I added a view UKDate
. 在导演Views/Shared/EditorTemplates
我添加了一个UKDate
视图。
The code for my particular view is: 我的特定视图的代码是:
@model DateTime?
@{
if (Model.HasValue)
{
var classname = ViewData["class"];
var id = ViewData["id"];
<input type="text"
name="@ViewData.TemplateInfo.HtmlFieldPrefix" //this needed to map the input to the property in your model, by the default model binder.
value="@Model.Value.ToString("dd/MM/yyyy")"
class="@classname"
id="@id"
/>
}
}
Then to use it I do the following: 然后使用它执行以下操作:
@Html.EditorFor(x => x.YourDateFieldDate, "UKDate", new { @class = "any css classes required here", id = "Id here" })
As there is now no data-val-date
attribute the validation is not run. 由于现在没有data-val-date
属性,因此无法运行验证。 So this solves the problem if you don't care about any client side date validation. 因此,如果您不关心任何客户端日期验证,就可以解决此问题。
Take a look to the example on this page, check if it does what you want. 看一下此页面上的示例,检查它是否满足您的要求。
https://johnnyreilly.github.io/jQuery.Validation.Unobtrusive.Native/AdvancedDemo/Globalize.html https://johnnyreilly.github.io/jQuery.Validation.Unobtrusive.Native/AdvancedDemo/Globalize.html
I saw you asked why you need to include jquery, the answer is because microsoft validation happens thanks to jquery, they work together to achieve this. 我看到您问为什么要包含jquery,答案是因为Microsoft验证要归功于jquery,因此他们共同努力实现了这一目标。
If you page is english en-US, default validation will work, but if your culture is different like mine, you need at least to include what is mentioned in that page (basically jquery, globalize and validation). 如果您的页面是英语(英语),则可以使用默认验证,但如果您的文化与我的不同,则至少需要包括该页面中提到的内容(基本上是jquery,globalize和validation)。
Globalize will override Validation to work in your desired culture. 全球化将覆盖验证以在您期望的文化中工作。
Give it a try and let me know. 试试看,让我知道。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.