dd / mm / yyyy日期验证MVC

时间:2016-09-15 18:07:47

标签: jquery asp.net-mvc validation

我创建了一个MVC应用程序,用户选择开始日期和完成日期。他们使用jQuery datepicker输入这些数据。但是当我点击保存时,我收到一条验证消息,说明它不是验证日期。 textbox接受日期格式yyyy / mm / dd。我不知道为什么我会收到这些验证消息,因为我尝试了很多东西。

我的代码如下

Create.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" />

1 个答案:

答案 0 :(得分:0)

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

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 date culture issue?