我已經決定開始使用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