I am currently working on a search page that includes several Select2 dropdowns. The dropdown works great on the first page load, but the issue only occurs when someone searches and then hits the browser back button and returns to the page. The cache values for the dropdowns/inputs are loaded and the issue occurs. All of my other form inputs load the cache completely normally with no issues.
After the "Back button" in their browser is hit, the search page with the Select2 dropdowns loads again, and the "cached" value that they searched for is stored from any entries they previously entered. The cached value is shown correctly for all input methods other than select2 on the page. When the dropdowns with Select2 load, the displayed text reverts back to "All Units" (meaning nothing selected), the placeholder I have set, yet the Select2 value is still set to what they just searched for.
This creates a confusing problem, and forces the user to search for what they previously had selected, with no ability to clear the dropdown.
Here is how the dropdown should look all the time (and how it looks upon first page load)
And here is how the select2 dropdown looks after the user hits the back button and returns to the page
It looks normal at first, but when you expand the dropdown you find the cached value selected but only if you expand the dropdown:
An item is selected (the one they previously selected) but it does not show it is selected in the actual value area, it still says "All Units". There is no way for the user to deselect this value or know it is still there. This leads to the user having to restart the page or clear the cache to be able to "unsearch" the select2 values
Here is my code, any help is much appreciated on this confusing issue.
@Html.DropDownListFor(model => model.UnitID, Model.unitIDL, new { @class =
"select2", style = "width: 165px" })
<script type="text/javascript">
$(".select2").select2(
{
allowClear: true,
placeholder: "All Units"
})
</script>