我的项目中有一个内容实体模型,它是我通过 EF6 创建的。
public partial class Content
{
public Content()
{
this.Comments = new HashSet<Comment>();
}
public int Id { get; set; }
public string Subject { get; set; }
[DataType(DataType.MultilineText)]
public string Brief { get; set; }
[UIHint("tinymce_full"), AllowHtml]
public string MainText { get; set; }
[DataType(DataType.DateTime)]
public System.DateTime DateOfPublish { get; set; }
[DataType(DataType.ImageUrl)]
public string SmallImageUrl { get; set; }
[DataType(DataType.ImageUrl)]
public string BigImageUrl { get; set; }
public string KeyWords { get; set; }
[DataType(DataType.DateTime)]
public System.DateTime DateOfExpire { get; set; }
public string AutherUserName { get; set; }
public long VisitVCount { get; set; }
public string Visible { get; set; }
public int ContentGroupId { get; set; }
public long LikeCount { get; set; }
public virtual ContentGroup ContentGroup { get; set; }
public virtual ICollection<Comment> Comments { get; set; }
}
如您所见,我有一列 发布日期 类型是 日期时间 .
我想用这个函数将发布日期转换为波斯日期:
public string ConvertToPersianToShow(DateTime? datetime)
{
string date;
DateTime dt;
if (!datetime.HasValue) return "";
dt = datetime.Value;
string year = Convert.ToString(persian_date.GetYear(dt));
string month = Convert.ToString(persian_date.GetMonth(dt));
string day = Convert.ToString(persian_date.GetDayOfMonth(dt));
if (month.Length == 1)
{
month = "0" + Convert.ToString(persian_date.GetMonth(dt));
}
if (day.Length == 1)
{
day = "0" + Convert.ToString(persian_date.GetDayOfMonth(dt));
}
Convert.ToString(persian_date.GetMonth(dt)) + "/" +
date = year + "/" + month + "/" + day;
return date;
}
所以我在我的视图(内容视图)中调用这个函数,所以我尝试使用这个函数用波斯语显示日期。所以我将英文日期传递给了我的函数,如下所示:
@{
ViewBag.Title = "Index";
DateConverter objconverter = new DateConverter();
}
<td>
@Html.DisplayFor(objconverter.ConvertToPersianToShow(modelItem => item.DateOfPublish ))
</td>
但是我得到了这个错误:
Argument type "lambda expression" is not assignable to parameter type 'system.nullable<system.datatime>'
此致敬意
@Html.DisplayFor(modelItem => objconverter.ConvertToPersianToShow(item.DateOfPublish ))
我想你想要:
@Html.DisplayFor(item => objconverter.ConvertToPersianToShow(item.DateOfPublish ))