我尝试将模型设备中的元素添加到模型WorkSheet中。首先,我使用设备创建表,但我不知道如何将所选设备添加到WorkSheet的代码按钮。 设备型号为:
public class Device
{
public int DeviceId { get; set; }
public string ShortcutName { get; set; }
public string SerialNumber { get; set; }
public string PrimarySupply { get; set; }
public string SecondarySupply { get; set; }
[DataType(DataType.Date, ErrorMessage = "Niepoprawny format daty")]
[DisplayFormat(DataFormatString = "{0:yyyy}", ApplyFormatInEditMode = true)]
public System.DateTime ProductionYear { get; set; }
[DataType(DataType.Date, ErrorMessage = "Niepoprawny format daty")]
[DisplayFormat(DataFormatString = "{0:yyyy}", ApplyFormatInEditMode = true)]
public System.DateTime AssemblyDate { get; set; }
[DataType(DataType.Date, ErrorMessage = "Niepoprawny format daty")]
[DisplayFormat(DataFormatString = "{0:dd.MM.yyyy}", ApplyFormatInEditMode = true)]
public System.DateTime LastReviewDate { get; set; }
[ForeignKey("Installation")]
public int InstallationId { get; set; }
public virtual Installation Installation { get; set; }
[ForeignKey("BatteryPack")]
public int BatteryPackId { get; set; }
public virtual BatteryPack BatteryPack { get; set; }
[ForeignKey("Load")]
public int LoadId { get; set; }
public virtual Load Load { get; set; }
[ForeignKey("OperatingMode")]
public int OperatingModeId { get; set; }
public virtual OperatingMode OperatingMode { get; set; }
[ForeignKey("DeviceModel")]
public int DeviceModelId { get; set; }
public virtual DeviceModel DeviceModel { get; set; }
[ForeignKey("DeviceType")]
public int DeviceTypeId { get; set; }
public virtual DeviceType DeviceType { get; set; }
public virtual ICollection<WorkSheet> WorkSheets { get; set; }
}
WorkSheet模型是:
public class WorkSheet
{
public WorkSheet()
{
}
public int? WorkSheetId { get; set; }
[ForeignKey("Device")]
public int DeviceId { get; set; }
public virtual Device Device { get; set; }
}
控制器:
public ActionResult WorkSheetAddDevice()
{
var devices = db.Devices.ToList();
var lista = devices.OrderBy(s => s.Installation.ShortcutName).Select(s => new DeviceListElement()
{
DeviceId = s.DeviceId,
ShortcutName = s.ShortcutName,
SerialNumber = s.SerialNumber,
PrimarySupply = s.PrimarySupply,
SecondarySupply = s.SecondarySupply,
ProductionYear = s.ProductionYear,
AssemblyDate = s.AssemblyDate,
LastReviewDate = s.LastReviewDate,
InstallationId = s.InstallationId,
Installation = s.Installation.ShortcutName,
BatteryPackId = s.BatteryPackId,
BatteryPack = s.BatteryPack.ShortcutName,
LoadId = s.LoadId,
Load = s.Load.Name,
OperatingModeId = s.OperatingModeId,
OperatingMode = s.OperatingMode.Name,
DeviceModelId = s.DeviceModelId,
DeviceModel = s.DeviceModel.Name,
DeviceTypeId = s.DeviceTypeId,
DeviceType = s.DeviceType.Name,
WorkSheets = s.WorkSheets.Count(),
});
return View(lista);
}
查看:
@model IEnumerable<ProjectE.Models.ViewModels.DeviceListElement>
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Installation)
</th>
<th>
@Html.DisplayNameFor(model => model.ShortcutName)
</th>
<th>
@Html.DisplayNameFor(model => model.DeviceModel)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Installation)
</td>
<td>
@Html.DisplayFor(modelItem => item.ShortcutName)
</td>
<td>
@Html.DisplayFor(modelItem => item.DeviceModel)
</td>
<td>
<input type="submit" value="Create" class="btn btn-default"/>
</td>
</tr>
}
</table>