tags:

views:

279

answers:

4

I'm trying to access the values in the FormCollection inside of an action. I can get the value field by doing:

var value = formCollection["MyDropDownList"];

But I can't seem to find a way to get the display value. Am I missing something obvious? A cast perhaps?

+1  A: 

Getting text from an HTML drop down selection list using JavaScript code

To get the text from each option is slightly trickier. We use the selectedIndex property of the selection list to capture the selected option and then pass this value to the options[].text property. Here is the code

var w = document.myform.mylist.selectedIndex;
var selected_text = document.myform.mylist.options[w].text;
mangokun
A: 

I don't think there's a way to get the display column from the formcollection. Basically, the formcollection is an easy way to interrogate the Request object (Request.Form, Request.QueryString, etc.) and the only thing that goes into that are values from input fields.

If you really need to get the display text, you would have to get it from whatever collection you bound the list with and access it via the key (your selected value from the formcollection). For example, if it's a dictionary collection that you bound to the list, use that same dictionary to lookup the value based on the key.

I would need to know more information as to how you're binding the dropdown to help you further.

Chris Conway
A: 

That's the normal behaviour. When a form is posted, only the name-value collection generated from the form fields are sent to the server. And of course the inner text of the option tag doesn't belong to that collection.

çağdaş
A: 

you do it ok, dropdown list sended shows value of selected item not displayed text of selected item...if you want(from some reason, because I am asumming you are filling that dropdown on model right? :)) to see send a display text also, maybe you can put it in hidden field with javascript on every change of selection in dropdown...

cheers

Marko