2011-08-16 24 views
5

我在我的MVC模型類上有一些DateTime字段 - 一些需要Date作爲輸入,另一些需要Time作爲輸入 - 但都是DateTime屬性。在MVC3中,可能有多個EditorTemplates用於同一類型?

是否有可能有一個EditorTemplate的DateTime,以某種方式產生一個日期選擇器的屬性,這意味着是日期,併爲屬性的時間選擇器的意思是時間?

回答

9

是的,這裏是一個辦法:

~/Views/Shared/EditorTemplates(或~/Views/Shared/DisplayTemplates,創建使用您喜歡的視圖引擎模板文件(例如使用剃刀/ C#)

文件Date.cshtml

replace this with a real date picker 

文件Time.cshtml

replace this with a real time picker 

然後,我n您的模型:

[UIHint("Date")] 
public DateTime DateProperty { get; set; } 

[UIHint("Time")] 
public DateTime TimeProperty { get; set; } 

UIHint屬性名稱必須匹配模板的文件名,UIHintSystem.ComponentModel.DataAnnotations,所以你需要適當的using語句/組裝的參考,如果你沒有它已經。

或者,使用TimeSpan來表示你的時間 - 這就是DateTime返回其TimeOfDay財產......

+0

感謝喬恩,我給一個嘗試。 –

相關問題