0

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".

tereško
  • 58,060
  • 25
  • 98
  • 150
gfrizzle
  • 12,419
  • 19
  • 78
  • 104
  • Did you check to make sure multiple items aren't being set as selected? (both in your ViewData["lob"] and in the actual html source) – James Avery Apr 16 '09 at 19:54
  • Perhaps I missing something, but how is the data bound to drop down in your code snippets. The "Nothing" in "...DropDownList("lob", Nothing ...", should it not be DropDownList("lob", lob..." where lob is a public or protected property in code-behind. – oglester Apr 17 '09 at 02:14
  • @James Avery - When it's working correctly, the appropriate item has selected="selected" in the markup. When I navigate via the link, none of the items are selected in the markup, which is why it's defaulting to the top of the list. – gfrizzle Apr 17 '09 at 13:01
  • @Greg Ogle - I'm using the helper functions. By naming the ViewData variable the same as the dropdown, it binds them. I tried taking out the rest of the variables after "lob", but it's still the same behavior. – gfrizzle Apr 17 '09 at 13:05
  • What is GetUserPreferenceLob returning? What if no user is logged in what does it return? – James Avery Apr 18 '09 at 02:26
  • GetUserPreferenceLob always returns a valid value when called (at least for this example). It's confirmed by the ViewData("message") display. – gfrizzle Apr 18 '09 at 23:26

3 Answers3

2

Did you try refreshing the page after following a link to the page. It could be a locally cached version.

oglester
  • 6,605
  • 8
  • 43
  • 63
  • It doesn't appear to have anything to do with caching. The page is never rendered with the first item in the list selected - why would there be a cached version of it? I also tried adding all the cache expiration code from another SO thread, and it didn't change anything. – gfrizzle Apr 16 '09 at 17:46
  • Just checking the more obvious possibilities. – oglester Apr 17 '09 at 01:56
0

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.

0

Probably caching ...

Check out this related thread here (Stack Overflow)

Community
  • 1
  • 1
  • I tried implementing the cache expiration code, but it didn't change anything. It doesn't appear to be cache related. – gfrizzle Apr 16 '09 at 17:46