问题描述
在我的mvc应用程序中,我使用 DisplayFor
Htmlhelper将
,如下所示。我试图像这样格式化。但是,我没有成功。 Date
赋值给我的视图
@ Html.DisplayFor(d => d.TravelAllowances.DepatureDate,{0: dd-MM-yy})
请给我一些方法来完成这个。
$ b
注意: DepartureDate
位于字符串
格式中。
感谢
Manikandan
我知道这个问题已经三年了, OP可能找到了一个方法,但万一别人偶然发现这个问题...
问题是,你只有一个字符串的日期。
如果您将它作为日期存储,并在从控制器填充传递给视图的模型时将其转换为字符串,则可以根据需要将其格式化为控制器。
如果你将日期存储为一个字符串(坏主意恕我直言),只是在控制器中填充模型属性,那么我会解析字符串到一个DateTime对象,然后将该DateTime格式化为模型属性的字符串。
仅仅是我的两分钱,晚了三年:)
In my mvc application, i assign the Date
to my view using DisplayFor
Htmlhelper
, like below. I tried to format like this. But, i am not successful.
@Html.DisplayFor(d => d.TravelAllowances.DepatureDate, "{0:dd-MM-yy}")
Please suggest me some way to accomplish this.
Note: DepartureDate
is in String
format.
ThanksManikandan
I know this question is three years old and the OP probably found a way, but in case others stumble upon this Q/A...
The problem is that you have the date as just a string.
If you are storing it as a date and converting it to a string when populating the model being passed to the view from the controller, then format it as you want in the controller.
If you are storing date as a string (bad idea IMHO), and just populating the model property in the controller, then I would parse the string into a DateTime object and then format that DateTime into a string for the Model's property.
Just my two cents, three years late :)
这篇关于日期格式与Html助手的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!