我刚刚实现了jQuery UI提供的datepicker,以便使用日历来选择日期。不幸的是,当我选择日期并希望单击“创建”按钮时,应用程序不希望保存我的条目,因为日期格式无效。我试图在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”格式,则会出现一个ohter验证错误,说“StartDate字段必须是日期。”
答案 0 :(得分:3)
您可以将模型的格式更改为:
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd-MM-yyyy}")]
public DateTime StartDate { get; set; }
修改:
或者从其他答案更新代码:
$(".datepicker").datepicker({
changeMonth: true,
changeYear: true,
dateFormat: 'dd-mm-yy'
});
答案 1 :(得分:1)
您可以使用dd/MM/yyyy
格式指定日期格式,尝试类似这样的示例:
$(".datepicker").datepicker({
changeMonth: true,
changeYear: true,
dateFormat: 'dd/MM/yyyy'
});
当您选择日期时,文本框上的值将以此格式显示,并在您的帖子上提交。
答案 2 :(得分: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;
}
答案 3 :(得分:-1)
你有像jquery.validator.js这样的插件吗? 如果你......试试这个:
$("#myform").validate({
ignore: "#DateInput"
})