I have a model, which has a List<SelectListItem>
which holds a list of Third Parties.
In my view, I need to iterate through the list, and create a DropDownList for that data, based on an id I have, called 'SelectedEntityId'.
I do the formal ForEach:
@foreach (var line in Model.Lines) {
And then, I attempt to render my drop down list:
@Html.DropDownListFor(x=>line.SelectedEntityId, new SelectList(Model.ThirdParties, "Value", "Text"), "Select One", new{@class="form-control"})
However, even though I see (in debug mode) that the 'SelectedEntityId' has the value of 5 (in this case), and that Model.ThirdParties has that item value - the drop down doesn't display that item, but rather its at 'Select One'.
I think it's possible my syntax x=>line.SelectedEntityId
is incorrect?
I am getting a warning on the word "line" therem saying "Access to foreach variable in closure. May have different behaviour when compiled with different versions of compiler."
Can I do that, in a foreach
, and reference the value of the item in my foreach
like that?