ryan
ryan

Reputation: 3

Giving an item in a list a new value using Razor

I am trying to give my ComponentID a new value when iterating through my list this is how I currently set it up

 @{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;
        }

As you can see I try assigning the value by doing this

 @Model.SubCompParts[sp].ComponentID == @subComp.ID; 
//also tried this @Model.SubCompParts[sp].ComponentID = @subComp.ID;

But this isn't working. How can I properly assign the value?

Upvotes: 0

Views: 33

Answers (1)

JuanR
JuanR

Reputation: 7793

Two equal signs are equality, not assignment. Also, you are already inside a code block but are switching contexts when you call the @Html.HiddenFor method. Change this:

@Model.SubCompParts[sp].ComponentID == @subComp.ID;

To this:

@{ Model.SubCompParts[sp].ComponentID = subComp.ID; }

There are several other things worth noting in your code but I will refrain from commenting on those.

Upvotes: 1

Related Questions