dd/mm/yyyy日期验证MVC



我制作了一个MVC应用程序,用户可以在其中选择开始日期和结束日期。他们使用jQuery日期选择器输入这些数据。然而,当我点击保存时,我会收到一条验证消息,说这不是验证日期。textbox接受日期格式yyyy/mm/dd。不知道为什么我会收到这些验证消息,因为我已经尝试了很多事情。

我的代码如下

创建.cshtml查看

      <div class="form-group">
            <script src="@Url.Content("~/Scripts/jquery-ui.min.js")" type="text/javascript"></script>
            <link href="@Url.Content("/Content/jquery-ui.min.css")" rel="stylesheet" type="text/css" />
            @Html.LabelFor(model => model.StartDate, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">

                <input type="text" id="MyDate"
                       @Html.EditorFor(model => model.StartDate, "{0:dd/MM/yyyy}", new { htmlAttributes = new { @class = "form-control" } })
                       @Html.ValidationMessageFor(model => model.StartDate, "", new { @class = "text-danger" }) />
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.FinishDate, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                <input type="text" id="FinishDate"
                       @Html.EditorFor(model => model.FinishDate, "{0:dd/MM/yyyy}", new { htmlAttributes = new { @class = "form-control" } })
                       @Html.ValidationMessageFor(model => model.FinishDate, "", new { @class = "text-danger" }) />
            </div>
        </div>

        <p>
            <button class="btn btn-success"><span class="glyphicon glyphicon-plus"></span>Create</button>
        </p>
        </div>
        }

<script type="text/javascript">
    $(document).ready(function () {
        $("#MyDate").datepicker();
            $.validator.methods.date = function (value, element) {
                Globalize.culture("en-AU");
                // you can alternatively pass the culture to parseDate instead of
                // setting the culture above, like so:
                // parseDate(value, null, "en-AU")
                return this.optional(element) || Globalize.parseDate(value) !== null;
            }
    });
</script>

<script type="text/javascript">
    $(document).ready(function () {
        $("#FinishDate").datepicker();
    });
</script>
<div>
    <a class=" btn btn-primary" href="@Url.Action("Index")"><span class=" glyphicon glyphicon-home"></span>&nbsp;Task List</a>
</div>

        @section Scripts {
            @Scripts.Render("~/bundles/jqueryval")
        }

我还尝试将其添加到webconfig

 <globalization culture="en-AU" uiCulture="en-AU" />

假设您具有以下模型属性:

public DateTime? StartDate { get; set; }
public DateTime? FinishDate { get; set; }

从你的上下文来看,你应该已经有了globalize.js和脚本目录中的globalize.culture.en-AU.js,因此您需要在编辑模式中应用预设日期格式,如下所示:

[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
[DataType(DataType.Date)]
public DateTime? StartDate { get; set; }
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
[DataType(DataType.Date)]
public DateTime? FinishDate { get; set; }

通过使用全球化插件覆盖jQuery基本验证,EditorFor将使用属性属性中给定的预设日期格式。

全球化代码应该类似于这个例子:

<script type="text/javascript">
    $(document).ready(function () {
        $("#MyDate").datepicker();
        $("#FinishDate").datepicker();
        $.validator.methods.date = function (value, element) {
                Globalize.culture("en-AU");
                // you can alternatively pass the culture to parseDate instead of
                // setting the culture above, like so:
                // parseDate(value, null, "en-AU")
                return this.optional(element) || Globalize.parseDate(value) !== null;
        }
    });
</script>

类似的问题可供进一步参考:MVC 4日期文化问题?

最新更新