使用这个模型:
public class Holiday
{
[Required]
public DateTime From { get; set; }
}
有了这个观点:
@model Holiday
@using System.Web.Optimization
@{
ViewBag.Title = "Request";
}
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Holiday Request</legend>
<div class="editor-label">
@Html.LabelFor(model => model.From)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.From)
@Html.ValidationMessageFor(model => model.From)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
如果我输入文本,我会得到一个无效的日期。如果我将该字段留空,我也会收到一条消息。但是,如果我输入的日期2013/20/20
是有效的,我该如何更改?我认为这可能与文化有关,但这在所有文化中都是无效的。
源 HTML
<form action="/" method="post">
<fieldset>
<legend>Holiday Request</legend>
<div class="editor-label">
<label for="From">From</label>
</div>
<div class="editor-field">
<input class="text-box single-line" data-val="true" data-val-date="The field From must be a date." data-val-required="The From field is required." data-val-weekdaydate="Must be a working day" id="From" name="From" type="datetime" value="" />
<span class="field-validation-valid" data-valmsg-for="From" data-valmsg-replace="true"></span>
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
</form>
<script src="/Scripts/jquery.unobtrusive-ajax.js"></script>
<script src="/Scripts/jquery.validate.js"></script>
<script src="/Scripts/jquery.validate.unobtrusive.js"></script>
</body>
</html>