Reputation: 12609
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".
Upvotes: 0
Views: 232
Reputation: 1
I have a very similar issue with TextBoxes:
On my page, I render a text box with Html.TextBox("Attribute", Model.Attribute). The user enters a value into this textbox, then presses a button. In the code that is executed, I compute a new value for the attribute of the model, and then render the view again. The Html.TextBox("Attribute", Model.Attribute) still displays the user-entered value, whereas Html.Encode(Model.Attribute) shows the correct value from the Model.
It seems as if Html.TextBox prefers to display the user-entered value rather than the programmer-modified value from the model. So instead of displaying the value of its second input parameter, Html.TextBox seems to display ViewData.ModelState["Atrribute"].Value.RawValue.
So I just change the view state and the model in the roundtrip.
Upvotes: 0
Reputation:
Probably caching ...
Check out this related thread here (Stack Overflow)
Upvotes: 0
Reputation: 6655
Did you try refreshing the page after following a link to the page. It could be a locally cached version.
Upvotes: 2