gpt4 book ai didi

c# - 如何在列表 C# MVC 中检索特定值

转载 作者:行者123 更新时间:2023-11-30 16:49:00 26 4
gpt4 key购买 nike

<分区>

我想根据列表本身的特定 Id 从列表中检索特定值。

这是我的列表值

    public List<strucMaterial> PopulateStructMaterialData()
{
List<strucMaterial> populateStructure = new List<strucMaterial>();
{

populateStructure.Add(new strucMaterial(){Id = 1, ifOthers = "", materialId = 1, materialNm = "Bricks", structId = 1, structNm = "Walls", insuranceReqId = 0});
populateStructure.Add(new strucMaterial(){Id = 2, ifOthers = "", materialId = 2, materialNm = "Concrete", structId = 1, structNm = "Walls", insuranceReqId = 0});
populateStructure.Add(new strucMaterial(){Id = 3, ifOthers = "", materialId = 3, materialNm = "Woods", structId = 1, structNm = "Walls", insuranceReqId = 0});

populateStructure.Add(new strucMaterial(){Id = 4, ifOthers = "", materialId = 2, materialNm = "Concrete", structId = 2, structNm = "Roof", insuranceReqId = 0});
populateStructure.Add(new strucMaterial(){Id = 5, ifOthers = "", materialId = 4, materialNm = "Tiles", structId = 2, structNm = "Roof", insuranceReqId = 0});
populateStructure.Add(new strucMaterial(){Id = 6, ifOthers = "", materialId = 5, materialNm = "Zinc", structId = 2, structNm = "Roof", insuranceReqId = 0});

populateStructure.Add(new strucMaterial(){Id = 7, ifOthers = "", materialId = 3, materialNm = "Woods", structId = 3, structNm = "Floor", insuranceReqId = 0});
populateStructure.Add(new strucMaterial(){Id = 8, ifOthers = "", materialId = 6, materialNm = "Reinforced Concrete", structId = 3, structNm = "Floor", insuranceReqId = 0});

};
ViewBag.populatebuilding = populateStructure;
return populateStructure;
}

我的 View 模型

public class strucMaterial
{
public int buildingInfoID { get; set; }
public int Id { get; set; }
public string ifOthers { get; set; }
public int materialId { get; set; }
public string materialNm { get; set; }
public int structId { get; set; }
public string structNm { get; set; }
public int insuranceReqId { get; set; }
public bool isSelected { get; set; }
}

我的看法

                  @{

List<Insurance.ViewModels.strucMaterial> viewModelSM = ViewBag.populatebuilding;

for (int i = 0; i < viewModelSM.Count; i++)
{
@Html.Label(viewModelSM[i].structNm)
@Html.CheckBox("structureMat", new { @id = "structureMat", @value = viewModelSM[i].Id })
@Html.Label(viewModelSM[i].materialNm)
<br />
}

}

这就是我检索 materialID 和 structID 的方式

                            int idArray = Convert.ToInt32(structureMat[i]);                     
List<strucMaterial> populateID = PopulateStructMaterialData();


for (int s = 0; s < populateID.Count; s++ )
{

if (populateID[s].Id.Equals(idArray))
{
int matID = populateID[s].materialId;
int structID = populateID[s].structId;

int a = matID;

}
}

它不是获取 materidID 和 structID 值,而是获取元素的索引值。我的问题是为什么它只读到 populateID[s]..?为什么不整体阅读 populate[s].materialId?

谢谢

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com