开发者

How do you pass all the contents of a ListBoxFor?

开发者 https://www.devze.com 2023-03-13 22:11 出处:网络
I am currently binding an IEnumerable collection to a ListBoxFor, which works as expected, sending the currently selected values on POST. However, I need to send all the values instead (essentially an

I am currently binding an IEnumerable collection to a ListBoxFor, which works as expected, sending the currently selected values on POST. However, I need to send all the values instead (essentially any value in a given ListBoxFor I consider to be required, whether selected or not). How would I go about doing this?

(I can probably rig something up in jQuery where, on-submit, it manually selects all the elements in a box, but was wondering if there was a be开发者_如何转开发tter way.)


If you want to continue using normal browser form serialization on submit, write a javascript function to fire right before the submission (hook into an onclick event or something) which iterates through the list box control and concatenates the desired values (perhaps comma-delimited) and places it in a hidden field. The value of that hidden field will be submitted normally and you can parse the individual values from it on the server side. It's still some manual work, but you avoid messing with GUI state (i.e. selecting all desired list box items) which I agree is something you don't want to do.

0

精彩评论

暂无评论...
验证码 换一张
取 消