我已经决定开始使用MVC 3,并且在尝试将我的一个Web应用程序重做为MVC3时遇到了此问题。Textboxfor 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。 所以首先:我怎样才能让日期格式化为shortdatestring? 我找到了MVC2 solution,建议在SharedFolder中创建EditorTemplates文件夹,并使用EditorFor代替,但这似乎不适用于MVC3。它似乎也可以防止像TextBox一样容易地添加类。
其次,一旦修复了这个问题,我就需要一个特殊的验证系统。我需要在开始日期之后有结束日期。我正在考虑使用javascript进行检查,发现http://www.datejs.com,但有没有办法直接在我的课上进行验证?
感谢您的联系!不过,我的日期仍然显示01/01/001。但我想我可以用jquery重写。 – LanFeusT 2011-02-18 01:50:53
可能是一个愚蠢的问题,但是被发送到视图之前填充日期? – DarkStar33 2011-02-18 15:17:28