I want to retrieve specific value from a list based on specific Id in the list itself.
This is my value of list
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;
}
My 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; }
}
My View
@{
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 />
}
}
This is how I retrieve the materialID and 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;
}
}
Instead of getting the materidID and structID value, it only the index value of the element. My question is why it only read until populateID[s]..? why not read populate[s].materialId as whole?
Thank you
Supplement to a Commentary Hari Prasad:
You can use LINQ and write populateID.First(x=>x.Id == idArray).materialId