[英]How to format the DateTime Property using MVC3 Razor?
I am having the following Razor Code to Render TextBox for ValidFrom Property of DateTime type. 我有以下Razor代码为DateTime类型的ValidFrom属性渲染TextBox。
@Html.TextBox("ValidFrom", (Model.ValidFrom.ToString("dd-MMM-yyyy")), new { @class = "FrmTextBox" })
And the same is rendering the Default Date(01-01-0001 00:00:00) as '01-Jan-0001' while opening of my Form and my values are also getting stored into the database once I submitted the form. 同样在打开表单时将默认日期(01-01-0001 00:00:00)呈现为'01 -Jan-0001',并且在我提交表单后,我的值也会存储到数据库中。
But, If I try to update the ValidFrom Property of the Subjected Record from the Database then the Record returning as 02-10-2012 00:00:00 but the Date is being displayed in the 02/10/2012 00:00:00 format only. 但是,如果我尝试从数据库更新Subjected Record的ValidFrom属性,则记录返回02-10-2012 00:00:00但日期显示在02/10/2012 00:00:00格式化。
How to format the above DateTime Value into 02-Oct-2012.? 如何将上述DateTime值格式化为2012年10月2日。
Could anyone please help.. 谁能请帮忙..
Thanks in Advance. 提前致谢。
Try to use the following attribute in your model. 尝试在模型中使用以下属性。
[DisplayFormat(ApplyFormatInEditMode = true,
DataFormatString = "specify_format_string_here")]
Then, on your view : @Html.EditorFor(m => m.ValidFrom)
. 然后,在您的视图上:
@Html.EditorFor(m => m.ValidFrom)
。 More about editor templates you can find for example here . 有关编辑器模板的更多信息,您可以在此处找到。 For your requirements format string should be smth like this:
对于您的要求,格式字符串应该像这样:
"{0:dd-MMMM-yyyy}"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.