在MVC

2012-02-07 60 views
1

表整理結果我有以下BO:在MVC

public class CinemaSchedule 
{  

    private DateTime showDate;   
    public DateTime ShowDate 
    { 
     get { return showDate; } 
     set { SetPropertyValue("ShowDate", ref showDate, value); } 
    } 

    private TimeSpan showTime;   
    public TimeSpan ShowTime 
    { 
     get { return showTime; } 
     set { SetPropertyValue("ShowTime", ref showTime, value); } 
    } 

    private Cinema cinema; 
    public Cinema Cinema 
    { 
     get { return cinema; } 
     set { SetPropertyValue("City", ref cinema, value); } 
    } 
} 

public class Cinema 
{ 

    private string code;  
    public string Code 
    { 
     get { return code; } 
     set { SetPropertyValue("Code", ref code, value); } 
    } 

    private string name;   
    public string Name 
    { 
     get { return name; } 
     set { SetPropertyValue("Name", ref name, value); } 
    } 
} 

在我寫控制器:

model.Schedules = FilmRepository.GetById(eventId).CinemaSchedules; 

在視圖中:

<% foreach (var schedule in film.CinemaSchedules) 
       { %> 
       <tr> 
        <td><a href="#"><%=schedule.Cinema.Name%></a></td> 
        <td class="time_list"> 
         <ul> 
          <li><a href="#"><%=TimeSpan.FromSeconds(schedule.ShowTime.Value) %></a></li>        
         </ul> 
        </td> 
       </tr> 
       <% } %> 

結果是:

Cinema 1 19:00:00 
Cinema 1 22:00:00 
Cinema 2 19:00:00 

但我想那是:

Cinema 1  19:00:00  22:00:00 
Cinema 2  19:00:00 

感謝。

+1

'ToLookup'就是你要的。我會留下來的。 – leppie 2012-02-07 10:48:05

+0

這看起來像一個樞軸 – 2012-02-07 10:53:25

回答

2

您想對電影執行GroupBy

確保System.Linq在您的視圖中被列爲使用。然後你可以做到以下幾點:

<% foreach (var scheduleGroup in film.CinemaSchedules.GroupBy(s => s.Cinema.Name)) 
{ %> 
<tr> 
    <td><a href="#"><%=scheduleGroup.Key%></a></td> 
    <td class="time_list"> 
     <ul> 
    <% foreach (var schedule in scheduleGroup) 
    { %> 
      <li><a href="#"><%=TimeSpan.FromSeconds(schedule.ShowTime.Value) %></a></li>        
    <% } %> 
     </ul> 
    </td> 
</tr> 
<% } %> 
+0

酷。十分感謝! – user348173 2012-02-07 11:03:15

+0

我建議您將該邏輯放入視圖模型中。 (具有'CinemaName'和'Times'(展平)屬性的視圖模型) – jgauffin 2012-02-07 11:38:24