Estoy usando asp.net c # mvc4
Actualmente puedo recuperar el valor de la opción seleccionada en mi controlador desde mi vista, pero también necesito recuperar el texto. Mi vista y controlador se ve así:
Ver:
<form method="post" action="myAction">
<select name="sel">
<option value="1" text="One">One</option>
<option value="2" text="Two">Two</option>
</select>
</form>
Controlador:
public ActionResult myAction(int sel)
{
int selValue = sel;
}
Este fragmento de código me funciona bien. Sin embargo, en mi controlador, quiero agregar string selText=something
para que también tenga el texto, y el código se vería así:
public ActionResult myAction(int sel, string something)
{
int selValue = sel;
string selText = something
}
¿Podría alguien ayudarme con esto?
Puede poner ambos (el valor y el texto) en el atributo de valor usando un separador.
<form method="post" action="myAction">
<select name="sel">
<option value="1|One">One</option>
<option value="2|Two">Two</option>
</select>
</form>
Y sepáralo en el controlador nuevamente.
public ActionResult myAction(string sel)
{
string[] selSplit = sel.Split('|');
int selValue = int.Parse(selSplit[0]);
string selText = selSplit[1];
}
Este artículo se recopila de Internet, indique la fuente cuando se vuelva a imprimir.
En caso de infracción, por favor [email protected] Eliminar
Déjame decir algunas palabras