我想根据列表中的特定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;
}
我的ViewModel
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] ..?为什么不读取[s] .materialId整体填充?
谢谢