Retrieve text (not value) from select option tag

Bivo Kasaju

I'm using asp.net c# mvc4

I can currently retrieve the value of selected option in my controller from my view, but I also need to retrieve the text. My View and Controller looks like this:

View:

<form method="post" action="myAction">
     <select name="sel">
          <option value="1" text="One">One</option>
          <option value="2" text="Two">Two</option>
     </select>
</form>

Controller:

public ActionResult myAction(int sel) 
{
     int selValue = sel;
}

This piece of code works fie for me. However, in my controller, I want to add string selText=something so that I have the text too, and the code would look like:

public ActionResult myAction(int sel, string something) 
{
     int selValue = sel;
     string selText = something
}

Could anyone help me with this?

Krisztián Balla

You could put both (the value and the text) into the value attribute using a separator.

<form method="post" action="myAction">
      <select name="sel">
          <option value="1|One">One</option>
          <option value="2|Two">Two</option>
     </select>
</form>

And separate it in the controller again.

public ActionResult myAction(string sel) 
{
    string[] selSplit = sel.Split('|');
    int selValue = int.Parse(selSplit[0]);
    string selText = selSplit[1];
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Add custom option (text + value) in a select tag

From Dev

Add custom option (text + value) in a select tag

From Dev

jQuery alert text in option from select tag

From Dev

Retrieve the value of the select option in a View

From Dev

Get Select Tag Value not Option Value jQuery

From Dev

Use JQuery to set the select option value and text from Array

From Dev

How I get the text instead value from a select option in CodeIgniter

From Dev

Use JQuery to set the select option value and text from Array

From Dev

use jQuery to select option from menu based on its text not value

From Dev

Angular select box with value differ from option text

From Dev

Set the value and text of a select option from Javascript table

From Dev

Find select option value by text

From Dev

Change option text in select tag just with CSS

From Dev

vbscript capture text in the html select option tag

From Dev

Align rendered option text in a select tag

From Dev

vbscript capture text in the html select option tag

From Dev

Change option text in select tag just with CSS

From Dev

jquery click select option value with option text

From Dev

Get the value of a text in select tag

From Dev

JQuery Find <option> tag by it value in <select>

From Dev

Is there a css selector to match a option value of the select tag?

From Dev

Ionic Select Option doesn' retrieve the value of option through ng-for

From Dev

Render select Tag from jQuery Ajax , then get the value from selected option

From Dev

Remove Blank option tag from select list

From Dev

Option value from select statement

From Dev

Remove option value from the select option

From Dev

Retrieve the value of certain tag from within the instance

From Dev

Retrieve text from list tag but don't calculate the child tag

From Dev

Displaying selected option from database in a select option tag

Related Related

  1. 1

    Add custom option (text + value) in a select tag

  2. 2

    Add custom option (text + value) in a select tag

  3. 3

    jQuery alert text in option from select tag

  4. 4

    Retrieve the value of the select option in a View

  5. 5

    Get Select Tag Value not Option Value jQuery

  6. 6

    Use JQuery to set the select option value and text from Array

  7. 7

    How I get the text instead value from a select option in CodeIgniter

  8. 8

    Use JQuery to set the select option value and text from Array

  9. 9

    use jQuery to select option from menu based on its text not value

  10. 10

    Angular select box with value differ from option text

  11. 11

    Set the value and text of a select option from Javascript table

  12. 12

    Find select option value by text

  13. 13

    Change option text in select tag just with CSS

  14. 14

    vbscript capture text in the html select option tag

  15. 15

    Align rendered option text in a select tag

  16. 16

    vbscript capture text in the html select option tag

  17. 17

    Change option text in select tag just with CSS

  18. 18

    jquery click select option value with option text

  19. 19

    Get the value of a text in select tag

  20. 20

    JQuery Find <option> tag by it value in <select>

  21. 21

    Is there a css selector to match a option value of the select tag?

  22. 22

    Ionic Select Option doesn' retrieve the value of option through ng-for

  23. 23

    Render select Tag from jQuery Ajax , then get the value from selected option

  24. 24

    Remove Blank option tag from select list

  25. 25

    Option value from select statement

  26. 26

    Remove option value from the select option

  27. 27

    Retrieve the value of certain tag from within the instance

  28. 28

    Retrieve text from list tag but don't calculate the child tag

  29. 29

    Displaying selected option from database in a select option tag

HotTag

Archive