【发布时间】:2011-06-29 08:48:28
【问题描述】:
我决定开始使用 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 solution,它建议在 SharedFolder 中创建一个 EditorTemplates 文件夹并改用 EditorFor ,但这似乎不适用于 MVC3 。它似乎也可以防止像使用 TextBoxFor 那样轻松添加类
其次,一旦解决了这个问题,我就需要建立一个特殊的验证系统。我需要让结束日期在开始日期之后。我正在考虑使用我找到的 http://www.datejs.com 的 javascript 进行检查,但有没有办法直接在我的班级上进行验证?
【问题讨论】:
标签: .net asp.net-mvc asp.net-mvc-3