将结果整理到MVC中的表中

时间:2012-02-07 10:46:39

标签: c# asp.net-mvc html-table

我有一个跟随BO:

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 个答案:

答案 0 :(得分: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>
<% } %>