分离的下拉列表日期时间Asp.net MVC [英] Separated Drop Down List Date Time Asp.net MVC
本文介绍了分离的下拉列表日期时间Asp.net MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从带有分隔下拉列表的用户那里花些时间。是否有效的代码在下拉列表项中定义日期变量。我是否需要在我的模型中单独定义它,如月,日,年。如果我这样做,我怎么能将统一的日期时间格式保存到我的数据库。
最好的代码方式是什么?
I want to take a time from user with separated drop down list. Is it efficieny code define date variables in drop down list item. Do i need to define separately it at my model like month,day,year. If i did this how can i save unified Date time format to my database.
What's is the best way code it ?
推荐答案
使用ModelBinder,我希望我会帮助你
Use ModelBinders like i hope i will help you
ModelBinders.Binders[typeof(DateTime)] = new DateTimeModelBinder()
{
Date = "Date", // Date parts are not splitted in the View
// (e.g. the whole date is held by a TextBox with id "xxx_Date")
Time = "Time", // Time parts are not splitted in the View
// (e.g. the whole time is held by a TextBox with id "xxx_Time")
Day = "Day",
Month = "Month",
Year = "Year",
Hour = "Hour",
Minute = "Minute",
Second = "Second"
};
了解更多详情,请看这个链接
HTTP:/ /dariosantarelli.wordpress.com/2010/12/26/asp-n et-mvc-2-splitting-datetime-in-drop-down-lists-and-model-binding / [ ^ ]
您可以在MVC中设置如下的常用日期格式
You can set a common date format in MVC like below
[DisplayName("date of birth")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public DateTime? Birth { get; set; }
希望这会有所帮助
Hope this helps
这篇关于分离的下拉列表日期时间Asp.net MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文