Monday, March 1, 2010

MVC Drop down list Problem with Form Post-back
Problem Description

“In Form post-back a drop down list with multiple selection selects only the first option in the drop down list although a view data of multiple selected values is posted to the page”.

Detailed Description
There is a problem of using a drop down list with “multiple = ‘true’“ attribute to have a multi-select drop down list using jQuery Dropdown Check List inside an Html Form.
I think that many of JQuery users would use it as it transforms a regular select html element into a dropdown checkbox list specially for multiple selections.

In aspx page I have something like this inside an Html form :

<%= Html.DropDownList("Modality", ViewData["ModalityList"] as MultiSelectList, new { id = "drpModality" })%>

This drop down list was used as a search header with a ‘Search’ submit button.

MultiSelectList selectionList = new MultiSelectList(selectListItems, "Value", "Text", SearchFields.StudyManager.Modality);
ViewData["ModalityList"] = selectionList;

I sent a ViewData["ModalityList"] from my controller to the page in the post back when search button clicked.
The problem is that the drop down list selects only the first item of the selections !!
After googling I found no solution for that problem and finally I resolved it.

Solution
The solution was very easy and effective. I only changed the drop down list to be a list box and it works perfectly.

<%= Html.DropDownList("Modality", ViewData["ModalityList"] as MultiSelectList, new { id = "drpModality" })%>

It seems that this is a bug ,but thanks to GOD I resolved it.
Any comments or questions are appreciated.

No comments:

Post a Comment