我有兩個模型串列和主模型。我需要訪問和存盤模型屬性的值。不知道如何使用 viewModel 來做到這一點?以前我使用的是 viewData 串列,現在我想測驗 viewModel。
<---Model--->
public class Master
{
public List<Table1> T1 { get; set; }
public List<Table2> T2 { get; set; }
}
public class Table1
{
public string sample1 { get; set; }
public string sample1 { get; set; }
}
public class Table2
{
public string sample1 { get; set; }
public string sample2 { get; set; }
}
<--Controller-->
Master master = new Master();
var getSamples = _db.dbSamples.Where(y => y.sample == "xxxx");
foreach(var row in T1) <<---- I need help correcting this part. I believe this is not right...
{
foreach (var item in getSamples)
{
row.sample1 = item.A;
row.sample2 = item.B;
}
}
return View(master);
uj5u.com熱心網友回復:
public class Master
{
public List<Table1> T1 { get; set; }
public List<Table2> T2 { get; set; }
}
public class Table1
{
public string sample1 { get; set; }
public string sample2 { get; set; }
}
public class Table2
{
public string sample1 { get; set; }
public string sample2 { get; set; }
}
<--Controller-->
Master master = new Master();
var getSamples = _db.dbSamples.Where(y => y.sample == "sample1");
var table1List = new List<Table1>();
var table2List = new List<Table2>();
var masterData= new Master();
foreach (var item in getSamples)
{
var data = new Table1();
data.A = item.A;
data.B = item.B;
table1List.add(data);
}
master.T1 = table1List;
var getSamples1 = _db.dbSamples.Where(y => y.sample == "sample2");
foreach (var item in getSamples1)
{
var data2 = new Table2();
data2.A = item.A;
data2.B = item.B;
table2List.add(data2);
}
master.T2 = table2List;
return View(master);
我假設你想要做的是這個。如果不是,請詳細說明問題。
更新了代碼。相應地更改變數名稱。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/498268.html
標籤:网 asp.net-mvc asp.net 核心
