We help IT Professionals succeed at work.

reselect Multiselect option

lulu50
lulu50 asked
on
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

Comment
Watch Question

ZvonkoSystems architect
CERTIFIED EXPERT
Top Expert 2006

Commented:

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?

lulu50Web application

Author

Commented:
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" } )
lulu50Web application

Author

Commented:
   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

Systems architect
CERTIFIED EXPERT
Top Expert 2006
Commented:

Hello lulu50,


you need not to set Selected to true.


And also no need for that jQuery script you posted in first code snippet box.


Change only this:

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


to this:

 @Html.DropDownListFor(m => m.SelectedVersion, Model.VersionList, new { @class = "form-control", @multiple = "multiple" } )



ZvonkoSystems architect
CERTIFIED EXPERT
Top Expert 2006

Commented:

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.

lulu50Web application

Author

Commented:
Thank you for all your help!!

it's working now