pass multiple values present in ListBoxFor to controller, while postback
I am using asp.net mvc 2.0 in my application and the application requires to get multiple values from a ListBox, from view to controller.
The scenerio is like that, I have two listbox say ListBox1 a开发者_如何学运维nd ListBox2 and on the load of a page, I initally load Listbox1 with some data.
Now a user can transfer some of the data from one Listbox1 to Listbox2 and then want to select multiple data present in ListBox2 in the controller.
I have used two "ListBoxFor" for this purpose and provide "IEnumerable" to the ListBox control in view as a property of a class.
Now when I post back the data to controller, I don't get the the data correspond to "ListBox2" in my class object.
In short, I want to select multiple data which is present in ListBox2, in my controller on postback of form.
Please help and provide sample for this.
An http post will not contain all values in a select box, only the one(s) selected. You could use javascript to select all values (make sure select is set to multiple), but that's not my preferred way since it changes the way the page looks to the user.
I would recommend using javascript to add the selected values to hidden fields. It would work something like this:
- Your model has a List selections
- on select, javascript is used to move from ListBox1 to ListBox2
- in addition, a new and the second
- On unselect, you would remove the input and update all inputs with a higher index to index - 1 in order to retain sequential-ness
Then when you post, the data will be automatically bound to your model's selections list.
精彩评论