我决定开始使用 MVC 3,并且在尝试将我的一个 Web 应用程序重做为 MVC3 时遇到了这个问题。
我的项目是这样设置的:
public class Project
{
public int ProjectID { get; set; }
[Required(ErrorMessage="A name is required")]
public string Name { get; set; }
[DisplayName("Team")]
[Required(ErrorMessage="A team is required")]
public int TeamId { get; set; }
[DisplayName("Start Date")]
[Required(ErrorMessage="A Start Date is required")]
[DisplayFormat(ApplyFormatInEditMode=true, DataFormatString="{0:d}")]
public DateTime StartDate { get; set; }
[DisplayName("End Date")]
[Required(ErrorMessage="An End Date is required")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:d}")]
public DateTime EndDate { get; set; }
}
我的报名表的日期是这样写的:
<table>
<tr>
<td>
<div class="editor-label">
@Html.LabelFor(model => model.StartDate)
</div>
</td>
<td>
<div class="editor-label">
@Html.LabelFor(model => model.EndDate)
</div>
</td>
</tr>
<tr>
<td>
<div class="editor-field">
@Html.TextBoxFor(model => model.StartDate, new { @class = "datepicker", id="txtStartDate" })
@Html.ValidationMessageFor(model => model.StartDate)
</div>
</td>
<td>
<div class="editor-field">
@Html.TextBoxFor(model => model.EndDate, new { @class = "datepicker", id = "txtEndDate" })
@Html.ValidationMessageFor(model => model.EndDate)
</div>
</td>
</tr>
</table>
我的问题是文本框现在显示 01/Jan/0001 00:00:00。
首先:如何将日期格式化为短日期字符串?
我找到了一个MVC2解决方案 https://stackoverflow.com/questions/2088139/how-to-format-the-value-in-a-strongy-typed-view-when-using-asp-net-mvcs-html-tex建议在 SharedFolder 中创建一个 EditorTemplates 文件夹并使用 EditorFor 代替,但这似乎不适用于 MVC3 。它似乎也阻止像 TextBoxFor 一样轻松添加类
其次,一旦解决了这个问题,我就需要建立一个特殊的验证系统。我需要将结束日期设置在开始日期之后。我正在考虑用我找到的 JavaScript 进行检查http://www.datejs.com http://www.datejs.com但有没有办法直接在我的班级上进行验证?
对于您的编辑模板,请尝试此操作
@model DateTime?
@Html.TextBox("", (Model.HasValue ? Model.Value.ToShortDateString() : string.Empty), new { @class = "datePicker" })
Source: 带日期时间的 MVC 3 编辑器模板 https://stackoverflow.com/questions/4249955/mvc-3-editor-template-with-datetime
jQuery 日期时间比较
$('#txtbox').change(function() {
var date = $(this).val();
var arrDate = date.split("/");
var today = new Date();
useDate = new Date(arrDate[2], arrDate[1] -1, arrDate[0]);
if (useDate > today) {
alert('Please Enter the correctDate');
$(this).val('');
}
});
Source: Jquery日期比较 https://stackoverflow.com/questions/4344351/jquery-date-comparison
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)