Link to home
Start Free TrialLog in
Avatar of lulu50
lulu50Flag for United States of America

asked on

reselect Multiselect option

Hi,

I'm creating a search page

1. on the page there is a multiselect dropdown list
2. I select some options from the list
3. I click on the submit button
4. after submitting the page my selection will get cleared out

How can I get my selection back once I click on the submit button?


 $('#SelectedVersion').multiselect({
            onInitialized: function (option, checked) {
                var dropdown = $('#SelectedVersion').siblings('.multiselect-container');
  
               //Here I want to reselect the options that I have already selected after submitting my form
               onInitialized: function (option, checked) {
                var values = [];
                var $SelectedOptions = $(this).find('option:selected');
                $SelectedOptions.each(function () {
                    values.push($(this).text());
                });
            },

            enableFiltering: true,
            buttonWidth: '350px',
            maxHeight: 200,
            enableCaseInsensitiveFiltering: true,
        }); 

Open in new window




<td>
                       @Html.DropDownListFor(m => m.SelectedVersion, new SelectList(Model.VersionList, "Value", "Text"), new { @multiple = "multiple" } )
                    </td>

Open in new window

Avatar of Zvonko
Zvonko
Flag of North Macedonia image

The page after submit is not cleared but fresh loaded and has no access to previous variables or page control states.

Therefore you need some options selcted information either from server or from stored cookies.

Can you send the options selected state from server after submit?

Avatar of lulu50

ASKER

Zvonko,

Thank you for responding to my question

yes the server is handling the selected return values.

they are in the SelectedVersion

@Html.DropDownListFor(m => m.SelectedVersion, new SelectList(Model.VersionList, "Value", "Text"), new { @multiple = "multiple" } )
Avatar of lulu50

ASKER

   public IEnumerable<SelectListItem> GetSelectedVersion(string[] SelectedVersions)
        {
            var GetVersion = _unitOfWorkCABusinessRules.RuleDetailRepo.GetAll().GroupBy(x => x.Version).Select(y => y.First()).Distinct();

            return GetVersion
                 .Select(x => new SelectListItem
                 {
                     Text = x.Version,
                     Value = x.Version.ToString(),
                   Selected = //here how can I say to select the ones in the SelectedVersions ?
        })
                .Distinct()
                .OrderBy(x => x.Text);
        }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Zvonko
Zvonko
Flag of North Macedonia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

Everything for keeping multi selecteion states selected by user is already done by the  @Html.DropDownListFor helper.

All additional code is only disturbing the helping states.

Avatar of lulu50

ASKER

Thank you for all your help!!

it's working now