The option text is not passed to the server. Either make the option value the same as the option text, or use the option value as a lookup key on the server, or populate a hiddenfield with the text on the client using jQuery when the user makes a selection:
ronaldorezen...
Member
28 Points
61 Posts
get select option selected
May 08, 2012 03:18 AM|LINK
I have the select element in a csthml page.
<select name="SelecaoStatus" class="SelecaoStatus">
How can I get the option text of this element on the same page?
The code @Request["SelecaoStatus"] give me the value. But now I need the option text.
Mikesdotnett...
All-Star
154818 Points
19853 Posts
Moderator
MVP
Re: get select option selected
May 08, 2012 04:51 AM|LINK
The option text is not passed to the server. Either make the option value the same as the option text, or use the option value as a lookup key on the server, or populate a hiddenfield with the text on the client using jQuery when the user makes a selection:
$(function(){ $('#SelecaoStatus').change(function(){ $('#myHidden').val($('#SelecaoStatus option[value=' + $(this).val() + ']').text()); }); });Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
ronaldorezen...
Member
28 Points
61 Posts
Re: get select option selected
May 08, 2012 10:02 PM|LINK
Hi Mike,
If the form hasn't been submitted yet, Is there a way to get the Select text or to get any element property using the Razor or ASP.NET resources?
Mikesdotnett...
All-Star
154818 Points
19853 Posts
Moderator
MVP
Re: get select option selected
May 09, 2012 04:35 AM|LINK
No. Razor and ASP.NET work on the server. If the form hasn't been submitted, it has yet to reach the server.
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
ronaldorezen...
Member
28 Points
61 Posts
Re: get select option selected
May 10, 2012 10:25 PM|LINK
Hi Mike,
In fact, I want to get the value when the page is loading.
For exemple, I want to get the selected SelecaoStatus text value after the code below:
<form action="" method="get"> <h4>Eventos do Grupo Bradesco Seguros <select name="SelecaoStatus" class="SelecaoStatus"> @foreach (var status in listaStatus){ <option @if(status.StatusId == statusIdPadrao){ <text>selected="selected"</text>; nomeStatusPadrao = status.Nome; } value="@status.StatusId"> @status.Nome </option> } </select> </h4>I already solved the problem using a variable (nomeStatusPadrao = status.Nome), but I want to know if it's possible to get values form some element.