我很沮丧。我有一系列表格,我试图将字段从一个模型显示到另一个模型的视图中。这是我的表结构 -
我正在尝试在DeviceLog的详细信息视图中显示设备,状态和位置字段。我创建了自己的设备模型。
public class DeviceLogIndexData
{
public IEnumerable<DeviceLog> DeviceLogs { get; set; }
public IEnumerable<DeviceStatu> DeviceStatus { get; set; }
public IEnumerable<DeviceLocation> DeviceLocations { get; set; }
public IEnumerable<Location> Locations { get; set; }
public IEnumerable<Status> Status { get; set; }
public IEnumerable<Device> Devices { get; set; }
}
已经有几种方法可以使用它。我的最新尝试是 -
var devicelog = new DeviceLogIndexData();
devicelog.DeviceLogs = db.DeviceLogs
.Include(d => d.Device)
.Include(d => d.Device.DeviceStatus.Select(s => s.Status))
.Include(d => d.Device.DeviceLocations.Select(x => x.Location))
.OrderBy(d => d.DeviceLogID);
devicelog = devicelog.DeviceLogs.Where(d => d.DeviceLogID == id.Value);
现在我对如何使用我的ViewModel感到非常沮丧和困惑。
答案 0 :(得分:0)
您想投影到视图模型
id
这是所有伪代码。所以你可能需要稍微玩一下。
您的视图模型类看起来像这样
var devicelog = db.DeviceLogs
.Include(d => d.Device)
.Include(d => d.Device.DeviceStatus.Select(s => s.Status))
.Include(d => d.Device.DeviceLocations.Select(x => x.Location))
.Where(d => d.DeviceLogID == id.Value)
.Select(d => new DeviceLogIndexData { Name = d.Device.Name, ... }).ToList();
然后将public class DeviceLogIndexData {
public string Name {get;set;}
public string Status {get;set;}
public string Location {get;set;}
}
返回到您的视图。