我希望能够根据列表中的特定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;
}
我的视图模型
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作为整体读取?
谢谢
populateID[s].materialId
è®؟é—®materialId
و—¶ï¼Œوک¯هگ¦ه‡؛çژ°ن»»ن½•é”™è¯¯ï¼ں - Hari Prasadmateriald
,则可以简单地执行以下操作:populateID.Where(x=>x.Id == idArray).Select(x=>x.materialId)
。 - Hari Prasad