2017-07-28 22 views
-1

我試圖格式化一個時間。我想以HH:mm格式顯示。我將一個字典傳遞給我的視圖,其中包含一個DateTime對象列表。它有這種格式:即使格式化時間顯示錯誤

var mapMovies = new Dictionary<string, List<DateTime>>(); 

很簡單。在我看來,這樣做:

foreach(var time in movie.Value) 
{ 
    @Html.DisplayFor(modelItem => time.ToString("{0:HH:mm}")) 
} 

當達到這一點,我得到了衆所周知的"Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions."例外。刪除ToString(「{0:HH:mm}」)它不正確顯示格式爲{0:dd/MM/yyyy HH:mm} 對我來說,再次格式化一個奇怪的事情是因爲在我的模型中,我已經做了即:

[DataType(DataType.Time)] 
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:HH:mm}")] 
public DateTime SessionTime { get; set; } 

如果我已經定義了我想在模型中顯示的方式,爲什麼會發生這種情況?

+1

取出支架。 – Gusman

+1

'time.ToString(「HH:mm」)'?如果使用'String.Format',則使用修飾符「{0}」。 –

+0

@Gusman非常相同的錯誤 – learner

回答

0

採用的解決方案是在控制器格式化時間

mapMovies[s.Movie.MovieName].Add(s.SessionTime.ToString("HH:mm"));