简体   繁体   English

在ajax.actionlink中格式化datetime对象

[英]format datetime object inside ajax.actionlink

I have a view where there are ajax.actionlinks, some of these action links need to display a date property of the model and I have the date property as follows: 我有一个ajax.actionlinks的视图,其中一些操作链接需要显示模型的date属性,而我的date属性如下:

[Display(Name = "Date")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:MM-dd-yyyy}", ApplyFormatInEditMode = true)]

public DateTime? Date { get; set; }

however, because ajax.actionlink accepts a string for its first argument, I can't use a lambda expression : 但是,因为ajax.actionlink的第一个参数接受一个字符串,所以我不能使用lambda表达式:

m => m.Date

rather I'm using 而是我正在使用

Model.Date.ToString()

but this isn't showing the formatting I want. 但这没有显示我想要的格式。 I've tried doing 我试着做

Model.Date.ToString("MM-dd-yyyy");

but I'm getting red underline because its not recognizing the ToString overload with 1 argument... any ideas on how I can get this to work? 但是我下划线是红色的,因为它不能识别1参数的ToString重载...关于如何使它起作用的任何想法?

Since Model.Date is nullable, you need to access the Value of the DateTime? 由于Model.Date可为空,因此您需要访问DateTime?Value DateTime? before using that version of ToString : 在使用该版本的ToString

Model.Date.HasValue ? Model.Date.Value.ToString("MM-dd-yyyy") : null;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM