Andrew Burns
Andrew Burns

Reputation: 14489

DropDownList with Firefox and ASP.NET MVC

I have been hitting a brick wall on this for about an hour now. I have a list of counties that I build and add to my view data (counties) and then render the list with a: html.DropDownList('invoice.county', counties) in my view.

It appears to render correctly but FF REFUSES to set the selected item. I have tried swapping the values out for integers (so they don't match the display text) and that did not work. FF just displays the first item in the list

<select id="invoice_county" name="invoice.county">
   ...
   <option value="Lander">Lander</option>
   <option selected="selected" value="Laramie">Laramie</option>
   <option value="Larimer">Larimer</option>
   ...
</select>

I have trimmed the values to the ones surrounding the selected item.

Can anyone give me insight into this????

Upvotes: 0

Views: 1461

Answers (2)

Fenton
Fenton

Reputation: 250922

If you are using XHTML, you need a valid attribute/value pair:

<option selected="selected" value="x">

If you are using HTML, the mere presence of the attribute is enough:

<option selected value="x">

More information on W3C.

Upvotes: 2

Francis Gilbert
Francis Gilbert

Reputation: 3442

Firefox has a weird bug/feature that means if you just refresh the page, it will select the option already selected regardless of whether the selected attribute is on another option. For example, if I put in:

<select id="invoice_county" name="invoice.county">
   <option value="Lander">Lander</option>
   <option selected="selected" value="Laramie">Laramie</option>
   <option value="Larimer">Larimer</option>
</select>

Saved and refreshed in Firefox, then put:

<select id="invoice_county" name="invoice.county">
   <option selected="selected" value="Lander">Lander</option>
   <option value="Laramie">Laramie</option>
   <option value="Larimer">Larimer</option>
</select>

instead and just refreshed after saving, it would keep "Laramie" selected. To stop this, try Ctrl-F5 rather than just F5 or refresh.

Upvotes: 7

Related Questions