2013-01-04 27 views
1

我有以下的時間跨度變量在我的模型類:ASP.NET MVC DisplayFormat爲小時

[DisplayFormat(DataFormatString = "{0:HH-mm}", ApplyFormatInEditMode = true)] 
public TimeSpan StartingHour { get; set; } 

但是,當我把它從我查看我得到了錯誤:

@Html.DisplayFor(modelItem => item.StartingHour) 

System.FormatException: Input string was not in a correct format.

的變量的值是:「18:00:00」。

應該是我DisplayFormat初始化怎麼辦?並請保留意見,以自己「從時間上算小時​​和分鐘分開」。

+0

嘗試將'@ Html.DisplayFor(modelItem => item.StartingHour)'更改爲'@ Html.DisplayFor(item => item.StartingHour)' –

+0

這不是問題所在。我用迭代「的foreach(在型號VAR項目)」 –

回答

3

嘗試:DataFormatString = @"{0:hh\-mm}"

+0

什麼是0:C做什麼呢?我已經可以在沒有DisplayFormat的情況下顯示它。編輯完成後:我已經嘗試過並得到相同的錯誤。 –

+0

@IsmetAlkan其實,看到我更新的答案。這應該工作。 – lahsrah

+0

你已經做了6次編輯。當我寫這篇文章時,你已經完成了第七項。請說出你的最終答案,或者在陳述和回答之前嘗試一次。 –

2

嘗試使用:

DataFormatString ="{0:hh\\:mm}" 

感謝。

相關問題