我正在开发一个MVC / EF Code-First应用程序。作为最后一项功能,我添加了一个历史记录表INV_AssetsHistory
,每当Insert/Update
中的记录为INV_Assets
时,该记录表都会有记录。我现在要弄清楚的是如何在我的视图中显示特定资产的每个INV_AssetsHistory
记录 - 比如用户选择Edit/Delete
的特定资产,我想要它们在INV_AssetsHistory
上查看视图中此特定INV_Assets
的每条记录。
有人能举例说明如何解决这个问题吗?根据我对MVC的经验,我认为我需要在两个表之间的某处添加一个外键属性,然后有一个ICollection<>
属性来保存所有INV_AssetsHistory
个记录,但是我不确定实现此功能的语法。
更新
我尝试了以下内容:
已添加到INV_Assets
:
public virtual ICollection<INV_AssetsHistory> AssetHistoryRecords { get; set; }`
更改INV_AssetsHistory
public int AssetId { get; set; }
到
public virtual INV_Assets INV_Asset { get; set; }
创建了新的Migration
,并运行了update-database
。
这导致:
介绍FOREIGN KEY约束&#39; FK_dbo.INV_AssetsHistory_dbo.INV_Assets_INV_Asset_Id&#39;在桌子上&#39; INV_AssetsHistory&#39;可能会导致循环或多个级联路径。指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY约束。无法创建约束。查看以前的错误。
更新2:
INV_Assets :
namespace InventoryTracker.Models
{
[GridTable(PagingEnabled = true, PageSize = 30)]
public class INV_Assets
{
// Setting GridColumn Annotations allows you to use AutoGenerateColumns on view to auto create the Grid based on the model.
public int Id { get; set; }
public int Model_Id { get; set; }
[ForeignKey("Model_Id")]
public virtual INV_Models Model { get; set; }
[Required]
public int Manufacturer_Id { get; set; }
[ForeignKey("Manufacturer_Id")]
public virtual INV_Manufacturers Manufacturer { get; set; }
[Required]
public int Type_Id { get; set; }
[ForeignKey("Type_Id")]
public virtual INV_Types Type { get; set; }
[Required]
public int Location_Id { get; set; }
[ForeignKey("Location_Id")]
public virtual INV_Locations Location { get; set; }
public int Vendor_Id { get; set; }
[ForeignKey("Vendor_Id")]
public virtual INV_Vendors Vendor { get; set; }
[Required]
public int Status_Id { get; set; }
[ForeignKey("Status_Id")]
public virtual INV_Statuses Status { get; set; }
public string ip_address { get; set; }
public string mac_address { get; set; }
[DataType(DataType.MultilineText)]
public string note { get; set; }
public string owner { get; set; }
//[DataType(DataType.Currency)]
//[DisplayFormat(DataFormatString="{0:C}", ApplyFormatInEditMode=true)]
[DisplayFormat(DataFormatString = "{0:#,###0.00}", ApplyFormatInEditMode=true)]
public decimal cost { get; set; }
public string po_number { get; set; }
[DataType(DataType.MultilineText)]
public string description { get; set; }
public int invoice_number{ get; set; }
[Required]
public string serial_number { get; set; }
[Required]
public string asset_tag_number { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime? acquired_date { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime? disposed_date { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime? verified_date { get; set; }
[Required]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime created_date { get; set; }
[Required]
public string created_by { get; set; }
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}")]
public DateTime? modified_date { get; set; }
public string modified_by { get; set; }
// Flag to specify if item is available? (Not signed out, not auctioned, recycled, etc.)
//public bool available { get; set; }
}
}
INV_AssetsHistory :
namespace InventoryTracker.Models
{
public class INV_AssetsHistory
{
public int Id { get; set; }
[Required]
public int AssetId { get; set; }
//public int Model_Id { get; set; }
//[ForeignKey("Model_Id")]
public virtual INV_Models Model { get; set; }
[Required]
//public int Manufacturer_Id { get; set; }
//[ForeignKey("Manufacturer_Id")]
public virtual INV_Manufacturers Manufacturer { get; set; }
[Required]
//public int Type_Id { get; set; }
//[ForeignKey("Type_Id")]
public virtual INV_Types Type { get; set; }
[Required]
//public int Location_Id { get; set; }
//[ForeignKey("Location_Id")]
public virtual INV_Locations Location { get; set; }
//public int Vendor_Id { get; set; }
//[ForeignKey("Vendor_Id")]
public virtual INV_Vendors Vendor { get; set; }
[Required]
//public int Status_Id { get; set; }
//[ForeignKey("Status_Id")]
public virtual INV_Statuses Status { get; set; }
public string ip_address { get; set; }
public string mac_address { get; set; }
[DataType(DataType.MultilineText)]
public string note { get; set; }
public string owner { get; set; }
//[DataType(DataType.Currency)]
//[DisplayFormat(DataFormatString="{0:C}", ApplyFormatInEditMode=true)]
[DisplayFormat(DataFormatString = "{0:#,###0.00}", ApplyFormatInEditMode = true)]
public decimal cost { get; set; }
public string po_number { get; set; }
[DataType(DataType.MultilineText)]
public string description { get; set; }
public int invoice_number { get; set; }
[Required]
public string serial_number { get; set; }
[Required]
public string asset_tag_number { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime? acquired_date { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime? disposed_date { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime? verified_date { get; set; }
[Required]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime created_date { get; set; }
[Required]
public string created_by { get; set; }
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}")]
public DateTime? modified_date { get; set; }
public string modified_by { get; set; }
}
}
答案 0 :(得分:1)
假设资产记录在SQL中具有id,您可以创建一个方法,使用资产ID从历史记录表中检索资产历史记录。也许是这样的事情。
只需传入assetId作为参数。
在您的数据存储库中
public IQueryable<INV_AssetsHistory> GetHistoryByAssetId(int assetId)
{
var records = _dataContext.INV_AssetsHistory.Where
(
x => x.AssetId == assetId
);
return records;
}
在您的控制器中
[HttpGet]
public ActionResult ViewAsset(int id = 0)
{
// Add Asset History to the View
ViewBag.History = _repository.GetHistoryByAssetId(id)
// Asset Details
var asset = _assetRepository.GetAssetById(id);
return View("AssetDetails", asset);
}
在您的视图中
<tbody>
@foreach (var action in ViewBag.History)
{
<tr>
<td>@action.something</td>
</tr>
}
</tbody>
如果我误解了你的问题,请随时纠正我。