0

我的程序中有以下视图。它有多个 FloorNum,但是当它显示时,它只显示第一个 FloorNum。如何循环它以便显示所有 FloorNum 的值,其中 LocID=xx

<table>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.LocID)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.FloorNum)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.RoomNum)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.RoomStatus)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model ) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.LocID)

        </td>
        <td>
            @Html.DisplayFor(modelItem => item.FloorNum)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.RoomNum)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.RoomStatus)
        </td>

        <td>

模型类是

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace HC.Data
{
    public class Rooms
    {
        [Key]
        public int ID { get; set; }
        public int LocID { get; set; }
        public int FloorNum { get; set; }
        public int RoomNum { get; set; }
        public int RoomStatus { get; set; }
    }
}

我无法将 LocID 更改为列表,因为所有工作都是使用 RAD 完成的,此时更改它会大大延迟。我只是想知道是否可以放置一些循环来使其工作。

4

2 回答 2

0

我的感觉是您将“房间”的单个值传递给视图,而 foreach 循环显示的是一个房间。如果您将 List 传递给视图,则 foreach 循环几乎肯定会像您编写的那样工作。但是 DisplayFor 可能不是因为它不再指向 Rooms 类的单个实例,而是指向房间列表。

于 2012-12-17T17:32:38.160 回答
0

假设您的模型是List<Room>

@{
    List<int> floors = Model.Select(x => x.FloorNum).Distinct();
}
@foreach (var floor in floors)
{
    foreach(var room in Model.Where(x => x.FloorNum == floor))
    {
        ... your display helpers here...
    }
}

这是伪代码,因此可能需要进行一些调整。

于 2012-12-17T17:43:43.523 回答