Tutorial :DropDownList with Firefox and ASP.NET MVC



Question:

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????


Solution:1

If you are using XHTML ...option selected="selected"... is correct. If you are using HTML, it should just be ...option selected... - and this is known to be a problem if you don't get it quite right.


Solution:2

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.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »