I'm having trouble choosing DropDownList. I read all the related entries and I cannot get a solution.
The actual approach seemed very good to me, because I can check the fields that will be inside the SelectList:
var selectList = new List<SelectListItem>( from variable in someKindOfCollection select new SelectListItem { Selected = variable.Property == selection, Text = variable.Property, Value = variable.Property });
Presumably this gives me complete control. After the selectList has been created, I can check the variables using the debugger. Everything is in order, and one of them has the selected attribute selected.
Then I use DropDownListFor to display on the view:
@Html.DropDownListFor( g => g.SomePropertyInModel , selectList, new { @class = "cssClass" })
But this does not work, never ... "Sends" a drop-down menu, but nothing is selected.
Many thanks:)
NEW EXAMPLE First of all, I want to apologize. I hid the information, of course, completely inadvertently. All code happens inside Razor For Loop:
@foreach (var loopVariable in Model.Collection) { if (Model.SomeCondition != null) { selection = someValue; } var selectList = new List<SelectListItem>( from variable in someKindOfCollection select new SelectListItem { Selected = variable.Property == selection, Text = variable.Property, Value = variable.Property }); @Html.DropDownListFor( g => g.SomePropertyInModel , selectList, new { @class = "cssClass" }) }
So, this is a fact of selectList - is it a local variable that causes behavior ?. Sorry, I didnβt think so.