3

我刚刚实现了 jQuery UI 提供的日期选择器,以便使用日历来选择日期。不幸的是,当我选择日期并想要单击创建按钮时,应用程序不想保存我的条目,因为日期格式无效。我试图在 webconfig 中添加全球化参数,但似乎这种方式不起作用。

jQuery部分:

@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
@Scripts.Render("~/bundles/jqueryui")
@Styles.Render("~/Content/themes/base/css")
<script type="text/javascript">
    $(document).ready(function () {
        $(".datepicker").datepicker({
            changeMonth: true,
            changeYear: true,

        });
    });
</script>

}

我的创建视图:

    @using (Html.BeginForm())
{
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Person</legend>
        <div class="editor-label">
            First name :
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(model => model.FirstName, new { maxlength = 50 })
            @Html.ValidationMessageFor(model => model.FirstName)
        </div>
        <div class="editor-label">
            Last name :
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(model => model.LastName, new { maxlength = 50 })
            @Html.ValidationMessageFor(model => model.LastName)
        </div>
        <div class="editor-label">
            National number :
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(model => model.NumNat, new { maxlength = 11 })
            @Html.ValidationMessageFor(model => model.NumNat)
        </div>
        <div class="editor-label">
            Start date :
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(model => model.StartDate, new { @class = "datepicker" })
            @Html.ValidationMessageFor(model => model.StartDate)
        </div>
        <div class="editor-label">
            End date :
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(model => model.EndDate, new { @class = "datepicker"})
            @Html.ValidationMessageFor(model => model.EndDate)
        </div>
        <div class="editor-label">
            Distance House - Work (km) :
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.HouseToWorkKilometers)
            @Html.ValidationMessageFor(model => model.HouseToWorkKilometers)
        </div>
        <div class="editor-label">
            Category :
        </div>
        <div class="editor-field">
            @Html.DropDownList("Id_ProductPackageCategory", "Choose one ...")
            @Html.ValidationMessageFor(model => model.Id_ProductPackageCategory) <a href="../ProductPackageCategory/Create">
                Add a new category?</a>
        </div>
        <div class="editor-label">
            Upgrade? :
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Upgrade)
            @Html.ValidationMessageFor(model => model.Upgrade)
        </div>
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

有什么想法可以解决这个问题吗?

编辑:当我修改并将我的日期格式设置为“dd/mm/yy”格式时,会出现一个验证错误,说“该字段 StartDate 必须是一个日期。” 如果我将格式更改为“mm/dd/yy”格式,则会出现一个验证错误,提示“字段 StartDate 必须是日期”。

4

4 回答 4

3

您也许可以将模型的格式更改为:

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd-MM-yyyy}")]
public DateTime StartDate { get; set; }

编辑:

或者将其他答案的代码更新为:

    $(".datepicker").datepicker({ 
   changeMonth: true,
   changeYear: true,
   dateFormat: 'dd-mm-yy' 
});
于 2013-03-14T15:34:02.363 回答
1

您可以指定日期的格式,尝试这样的操作,例如使用以下dd/MM/yyyy格式:

$(".datepicker").datepicker({ 
   changeMonth: true,
   changeYear: true,
   dateFormat: 'dd/MM/yyyy' 
});

当您选择日期时,文本框上的值将以这种格式出现并提交到您的帖子中。

于 2013-03-14T15:19:24.503 回答
0

希望这可以帮助您,您可以更改 Global.asax 文件中的当前文化,用于应用程序级别例如,

using System.Globalization;
using System.Threading;

protected void Application_BeginRequest(Object sender, EventArgs e)
{    
  CultureInfo newCulture = (CultureInfo) System.Threading.Thread.CurrentThread.CurrentCulture.Clone();
  newCulture.DateTimeFormat.ShortDatePattern = "dd-MMM-yyyy";
  newCulture.DateTimeFormat.DateSeparator = "-";
  Thread.CurrentThread.CurrentCulture = newCulture;
}
于 2016-02-02T11:52:00.223 回答
-1

你有像 jquery.validator.js 这样的插件吗?如果你...试试这个:

 $("#myform").validate({
   ignore: "#DateInput"
})
于 2016-06-13T18:52:58.497 回答