2014-04-04 33 views
1

我有一個內容實體模型在我創建它的EF6項目中。在MVC4視圖中轉換datetime導致錯誤

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; } 
} 

正如你可以看到我有一列DateOfPublish和類型是日期時間

我想把日期轉換DateOfPublish波斯日期使用此項功能:

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; 
     } 

因此,我調用這個函數在我看來(內容視圖),所以我試圖表明在波斯日期使用這種function.So我通過了英語日期到我的功能,你可以在這裏看到:

@{ 
    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>' 

問候

回答

1
@Html.DisplayFor(modelItem => objconverter.ConvertToPersianToShow(item.DateOfPublish)) 
+1

謝謝它的工作原理 –

+0

現在我得到這個錯誤在該行,你說:模板只能與現場使用訪問權限,屬性訪問權限,單維數組索引或單參數自定義索引器表達式。 –

0

我想你想:

@Html.DisplayFor(item => objconverter.ConvertToPersianToShow(item.DateOfPublish)) 
相關問題