C# MVC 日期格式和日期选择器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15413141/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MVC date format and datepicker
提问by Traffy
I just implemented the datepicker provided by jQuery UI in order to use the calendar to choose the a date. Unfortunately, when I choose the date and want click on the create button, the application doesn't want to save my entries because the date format is invalid. I tried to add the globalization parameter in the webconfig but it seems that this way doesn't work.
我刚刚实现了 jQuery UI 提供的日期选择器,以便使用日历来选择日期。不幸的是,当我选择日期并想要单击创建按钮时,应用程序不想保存我的条目,因为日期格式无效。我试图在 webconfig 中添加全球化参数,但这种方式似乎不起作用。
The jQuery part :
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>
}
}
My create View :
我的创建视图:
@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>
}
Any idea to resolve this problem?
任何想法来解决这个问题?
EDIT : When I modify and put my date format as a "dd/mm/yy" format, an validation error appears saying "The field StartDate must be a date." and if I change the format as a "mm/dd/yy" format, an ohter validation error appears saying "The field StartDate must be a date."
编辑:当我修改日期格式并将其设置为“dd/mm/yy”格式时,会出现验证错误,提示“字段 StartDate 必须是日期”。如果我将格式更改为“mm/dd/yy”格式,则会出现其他验证错误,提示“字段 StartDate 必须是日期”。
回答by Felipe Oriani
You could specify the format of your date, try something like this, for sample using the dd/MM/yyyy
format:
您可以指定日期的格式,尝试这样的操作,例如使用以下dd/MM/yyyy
格式:
$(".datepicker").datepicker({
changeMonth: true,
changeYear: true,
dateFormat: 'dd/MM/yyyy'
});
And when you select a date, the value on the textbox will apper in this format and submit it on your post.
当您选择一个日期时,文本框上的值将采用此格式并将其提交到您的帖子中。
回答by Michael Fürstenberg
You could perhaps change the format for your model to something like:
您也许可以将模型的格式更改为:
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd-MM-yyyy}")]
public DateTime StartDate { get; set; }
Edit:
编辑:
Or update the code from the other answer to this:
或者从另一个答案更新代码:
$(".datepicker").datepicker({
changeMonth: true,
changeYear: true,
dateFormat: 'dd-mm-yy'
});
回答by Ravi Mehta
May this can help you ,You can change the current culture in your Global.asax file, for application level For Example,
可能这可以帮助您,您可以更改 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;
}
回答by Miguel Angel
Did you have any plugin like jquery.validator.js ? If you... try this:
你有像 jquery.validator.js 这样的插件吗?如果你......试试这个:
$("#myform").validate({
ignore: "#DateInput"
})