2012-08-08 264 views
-2

http://thewayofcode.wordpress.com/2012/01/18/custom-unobtrusive-jquery-validation-with-data-annotations-in-mvc-3/MVC3日期驗證

我試圖從上面的鏈接工作,但不能得到它正確的示例。我不確定我做錯了什麼?

我複製的模型驗證

[DataType(DataType.Date)] 
    [DisplayName("Start Date of Construction")] 
    [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)] 
    public DateTime? StartDate { get; set; } 

[DataType(DataType.Date)] 
[DisplayNameAttribute("End Date of Construction")] 
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)] 
[DateGreaterThan("StartDate", "Estimated end date of construction must be greater than the start date of the construction")] 
public DateTime? EndDateOf { get; set; } 

其次DateGreaterThan代碼。

在查看

@Html.EditorFor(model => model.StartDate) 
    @Html.EditorFor(model => model.EndDate) 
    @Html.ValidationMessageFor(model => model.StartDate) 
    @Html.ValidationMessageFor(model => model.EndDate) 

但沒有火災的按鈕點擊

,如果有人能提供一些線索light.It將是非常有益的,甚至這樣做是appreciated.Similarly試圖獲得的任何其他不同的方式在下拉列表中選擇一個值並在文本框中輸入值時,從Model進行驗證工作。

+0

我們可以看到您的控制器代碼在哪裏發佈表單數據? – Tommy 2012-08-08 21:49:19

+0

您正在關注的示例的源代碼可以從[here](https://github.com/vgheri/DateCustomValidationExample)下載。下載它,並與您嘗試過的內容進行比較,這樣您就可以更快地找到解決方案,更重要的是,您將很樂意自己找出問題所在。相信我 ! – Yasser 2012-08-09 05:25:39

回答

1
  1. 而不是[DisplayName("Start Date of Construction")]應使用而不是[DisplayNameAttribute("End Date of Construction")][Display(Name="Start Date of Construction")]
  2. 應該使用[Display(Name="End Date of Construction")]

看看你至少該運行。您的數據註釋屬性會混亂,只需在Web上查找示例即可。

+0

請讓我知道這是否有任何幫助!謝謝。 – 2013-02-05 23:19:18