Here is a stripped-down code snippet from a MVC3/Razor view file:
@foreach (var item in Model.Stuff.Items){
<tr>
<td>@item.Title</td>
</tr>
<tr>
<td>
@using (Html.BeginForm()) {
@item.Title
@Html.HiddenFor(item => item.Title)
}
</td>
</tr>
} @* End of Items foreach loop *@
The title shows on the first row.
It also shows inside the form. But trying to use it in the HiddenFor I get error CS0136: A local variable named ‘item’ cannot be declared in this scope because it would give a different meaning to ‘item’, which is already used in a ‘parent or current/child’ scope to denote something else
I don’t get why that would be the case; in item => item.Title the first “item” is effectively just a parameter name in an anonymous function, isn’t it?
When I change it to: @Html.HiddenFor(s => s.Title) I get error CS1963, An expression tree may not contain a dynamic operation.
Background: the intention is to have two table rows per entry in Model.Stuff.Items, the first giving current information as static HTML, the second an edit form for it. (One or the other will be hidden at any time using javascript.) The form will submit to another action on this controller. All the forms submit to the same URL; the hidden values will identify which row is being updated.
Use
@Html.HiddenFor(x => item.Title)