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?
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.
Comments