在我的 Web 應用程式中,我需要在不同表格的 html 視圖中顯示兩種型別的資料。
所以首先我創建了兩個不同的視圖模型來存盤資料。
所以在控制器中,我撰寫了這段代碼來將資料發送到視圖模型
List < RecognizedPartsViewModel > Reco = new List < RecognizedPartsViewModel > ();
var rData = (from i in db.InventoryMain join p in db.PartCategory on i.PartCatogary equals p.Id where i.ReOrderQty != 0 && i.AvaQty <= i.ReOrderQty && i.PartCatogary != 0 select new RecognizedPartsViewModel {
PartNo = i.PartNo,
Description = i.PartDescription,
Model = i.PartModel,
AvaQty = i.AvaQty,
ReOrderQty = i.ReOrderQty,
PartCato = i.PartCatogary,
ABCD = i.A_B_C_D_Category
}).ToList();
List < UnRecoPartsViewModel > unReco = new List < UnRecoPartsViewModel > ();
var rUnData = (from i in db.InventoryMain where i.ReOrderQty != 0 && i.AvaQty <= i.ReOrderQty && i.PartCatogary == 0 select new UnRecoPartsViewModel {
PartNo = i.PartNo,
Description = i.PartDescription,
Model = i.PartModel,
AvaQty = i.AvaQty,
ReOrderQty = i.ReOrderQty,
ABCD = i.A_B_C_D_Category
}).ToList();
所以我需要知道我必須在同一個 html 視圖中顯示這些資料。那么如何在 html 視圖中將這兩個視圖模型稱為單獨的串列呢?
uj5u.com熱心網友回復:
如果您的資料模型具有問題中描述的結構,您可以將一個類用于兩種情況:
public class PartsViewModel
{
public string PartNo { get; set; }
public string Description { get; set; }
public string Model { get; set; }
public int AvaQty { get; set; }
public int ReOrderQty { get; set; }
public string PartCato { get; set; } // Used only in the a table for RecognizedParts
public string ABCD { get; set; }
}
然后創建一個新類以包含每個類別的分隔串列:
public class PartsDataModel
{
public List<PartsViewModel> RecognizedParts { get; set; }
public List<PartsViewModel> UnRecognizedParts { get; set; }
}
然后:
public ActionResult Index()
{
// ... copy your previous code here, but use `PartsDataModel` class
// instead of `RecognizedPartsViewModel `UnRecoPartsViewModel`.
var model = new PartsDataModel()
{
RecognizedParts = rData,
UnRecognizedParts = rUnData
}
// Create the view
return View(model);
}
uj5u.com熱心網友回復:
您需要創建另一個可以接受兩種型別資料的模型,即 1List < RecognizedPartsViewModel >和 1,List < UnRecoPartsViewModel >并且您將將此模型傳遞給您的視圖并使用此模型訪問您的每個串列
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/407665.html
標籤:
