0

I have the following properties

public SelectList ListActivities { get; set; } // Will load all hobbies i,e football, golf etc and will be displayed in a dropdown

public List<string> SelectedActivities { get; set; } // Trying to set with multiple selected values.

This is my view.

 <div class="col-lg-11">
   @Html.DropDownListFor(m => m.UserDetails.SelectedActivities, Model.UserDetails.ListActivities, "Please Select", new { @class = "form-control", multiple = "multiple", id = "listActivities" })
 </div>

The issue I have is when I selected more then one option from the ActivitiesDropdown and press submit on my page and go back to the controller the SelectedActivities is null.

Can one shed some light on this please?

Code Ratchet
  • 5,758
  • 18
  • 77
  • 141
  • possible duplicate of [Getting Multiple Selected Values in Html.DropDownlistFor](http://stackoverflow.com/questions/12176735/getting-multiple-selected-values-in-html-dropdownlistfor) – Brad Christie Apr 03 '15 at 03:51
  • have tried viewing Request Parameters being submitted in Firebug or developer tools. Check if value is being submitted correctly or not. – Shoaib Shakeel Apr 03 '15 at 04:41
  • The code you have shown here works fine. Note it makes no sense to include the `Please Select` parameter for a multiple select (if it was selected you would get a `null` value in `SelectedActivities `) and you should use `ListBoxFor()` which adds the `multiple` attribute for you. –  Apr 06 '15 at 05:38

1 Answers1

0

For multi-select you should use Html.ListBoxFor and not a Html.DropDownListFor because Html.DropDownListFor returns a single-selection select element.

So for this to work just change your view to:

 <div class="col-lg-11">
   @Html.ListBoxFor(m => m.UserDetails.SelectedActivities, Model.UserDetails.ListActivities, "Please Select", new { @class = "form-control", id = "listActivities" })
 </div>
Alex Art.
  • 8,711
  • 3
  • 29
  • 47
  • `@Html.DropDownListFor()` with `multiple = "multiple"` is identical to `@Html.ListBoxFor()` - both generate a ` –  Apr 06 '15 at 05:34
  • You are absolutely right. I didn't notice the usage of `multiple` attribute – Alex Art. Apr 06 '15 at 07:14