Domanda

This is my Razor view in ASP.NET MVC3 Application

@Html.DropDownListFor(m => m.Leave_Type, Model.Leave_Types, new { @class = "input-append" })

On POST when I read Model.LeaveType, it returns selected value. I want to get the selected text instead, how do I get it? I appreciate any help. Thanks a lot in advance!!

È stato utile?

Soluzione

The answer is.. it depends. A drop down is just a standard HTML dropdown control, and it can only post the selected value. It will never post the text, and there is no way to change that.

Do you need the selected value as well? If so, then you can't get both like that. You would have to look up the Leave_Type text in the Leave_Types object based on the selected value returned.

If you don't need the Leave_Type, then you can simply use the text for both the value and text.

Another option might be to write some javascript that copies the selected item text to a hidden field whenever the value of the dropdown changes. Then that hidden field will be posted and you can check that.

I would, however, simply look up the value in whatever method you use to create the Leave_Types collection in the first place.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top