I've got a simple search page on my Index view with a dropdown and a text box. I'd like to remember the user's preference for the dropdown, so I store that in a table and retrieve it as needed. Here's the Controller function:
Function Index(ByVal lob As String, ByVal filter As String) As ActionResult
If If(lob, "") = "" Then
lob = GetUserPreferenceLob()
End If
ViewData("lob") = New SelectList(GetLobValues(), "Value", "Text", lob)
ViewData("message") = lob
Return View()
End Function
The View looks like this:
<% Using Html.BeginForm()%>
Line of Business:
<%=Html.DropDownList("lob", Nothing, New With {.onchange = "document.forms[0].submit()"})%>
Search:
<%=Html.TextBox("filter")%>
<img src="..." alt="Search" onclick="document.forms[0].submit()" />
<%=ViewData("message")%>
<% End Using%>
When I start the app (this is the default page), it successfully loads the list and selects the user's item. If I navigate to the page however, like via a link elsewhere on the page, it loads the list but selects the first item by default. I've run the debugger and it's always going through the Index function, and according to the "message" output it's always passing the right value to be selected, so why is that scenario not selecting the right entry in the list?
Update: I've got other data on the form that depends on the selected value from the dropdown. When navigating to the page via a link, the rest of the page is behaving as if the appropriate item is selected, but the dropdown defaults to the top. In other words, if my dropdown has the values "A", "B", and "C", and I select "C", then click a link that reloads the page, the dropdown shows "A", but the rest of the page has the data for "C".