2011-02-17 73 views
13

我已經決定開始使用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,但有沒有辦法直接在我的課上進行驗證?

回答

8

爲您編輯模板試試這個

@model DateTime? 
@Html.TextBox("", (Model.HasValue ? Model.Value.ToShortDateString() : string.Empty), new { @class = "datePicker" }) 

來源: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(''); 
      } 
     }); 

來源:Jquery date comparison

+0

感謝您的聯繫!不過,我的日期仍然顯示01/01/001。但我想我可以用jquery重寫。 – LanFeusT 2011-02-18 01:50:53

+0

可能是一個愚蠢的問題,但是被髮送到視圖之前填充日期? – DarkStar33 2011-02-18 15:17:28

0

// datimetime在日期選擇器顯示爲11/24/2011 12:00:00 AM

//你可以通過空間分割這個並將其值設置迄今爲止只有

腳本:

if ($("#StartDate").val() != '') { 
     var arrDate = $('#StartDate').val().split(" "); 
     $('#StartDate').val(arrDate[0]); 
    } 

標記:

<div class="editor-field"> 
     @Html.LabelFor(model => model.StartDate, "Start Date") 
     @Html.TextBoxFor(model => model.StartDate, new { @class = "date-picker-needed" }) 
    </div> 

希望這有助於..

6

這樣做有一個相當簡單的方法。只需使用一個TextBox而非TextBoxFor

@Html.TextBox("ReturnDepartureDate", 
    Model.ReturnDepartureDate.ToString("dd/MM/yyyy")) 

這有將所有的不顯眼的JavaScript元素等也少代碼的優勢!

2

如果你用以下裝飾你的viewmodel對象,你只能看到文本框中的日期部分。

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:d}")] 
public DateTime Date { get; set; } 

那麼你的頁面上,你可以簡單地做以下

@Html.EditorFor(model => model.Date) 

注意,這是使用標準MVC3。我沒有自定義日期編輯器模板。

此答案一切歸功於assign date format with data annotations

3

看一看這個例子

@Html.TextBoxFor(s => s.dateofbirth, new { Value = @Model.dateofbirth.ToString("dd/MM/yyyy") }) 
0

太晚了,但可能是它可以幫助別人:

型號:

[Display(Name = "Date of birth")] 
public DateTime? DOB{ get; set; } 

HTML:

@Html.TextBoxFor(m => m.DOB, "{0:MM/dd/yyyy}", 
      new { @placeholder="MM/DD/YYYY", @au19tocomplete="off" }) 

JavaScript的日曆添加到控件:

$('#DOB').datepicker({changeMonth: true, changeYear: true, 
     minDate: '01/01/1900', maxDate: new Date});