问题描述
在尝试遍历列表时,我试图给我的ComponentID
一个新值,这是我目前设置的方式
@{Component subComp = db.Components.Find(Model.ComponentSubComps[c].ID);}
@for (int sp = 0; sp < subComp.CHP.Count; sp++)
{
Part sPart = db.Parts.Find(Model.SubCompParts[sp].ID);
@Html.HiddenFor(x => x.SubCompParts[sp].PartID)
@Model.SubCompParts[sp].ComponentID == @subComp.ID;
}
如您所见,我尝试通过此操作分配值
@Model.SubCompParts[sp].ComponentID == @subComp.ID;
//also tried this @Model.SubCompParts[sp].ComponentID = @subComp.ID;
但这不起作用。如何正确分配值?
解决方法
两个等号是相等,而不是赋值。另外,您已经在代码块中,但是在调用@Html.HiddenFor
方法时切换上下文。更改此:
@Model.SubCompParts[sp].ComponentID == @subComp.ID;
对此:
@{ Model.SubCompParts[sp].ComponentID = subComp.ID; }
您的代码中还有其他值得注意的地方,但我将不对此发表评论。