Save to a variable selected value of a select

0

I wanted to know how I can save the selected value of a select in a variable, and then send it to the controller. I'm working with MVC C #

VISTA

@ {     Layout = null;

}               

<title>MESES</title>

    <form id="form1" method="post">

        <fieldset>
            <legend>Selecciones mes</legend>
                        <select name="selectmes" id="mes">
                            <option value="1">Enero</option>
                            <option value="2">Febrero</option>
                            <option value="3">Marzo</option>
                            <option value="4">Abril</option>
                            <option value="5">Mayo</option>
                            <option value="6">Junio</option>
                            <option value="7">Julio</option>
                            <option value="8">Agosto</option>
                            <option value="9">Septiembre</option>
                            <option value="10">Octubre</option>
                            <option value="11">Noviembre</option>
                            <option value="12">Diciembre</option>

                        </select>


                    <p>
                        <input id="BtnEnviar" name="Button1" type="submit" alue="enviar" onclick="return validar()" />
                    </p>
        </fieldset>
</form>

Thank you in advance

    
asked by Andres 03.01.2018 в 15:36
source

1 answer

0

In Asp MVC we have something called Model Binding which consists in obtaining all the html elements with value in a collection in a form post action (FormCollection).

In your case you can do something like this:

[HttpPost]
public ActionResult Index(FormCollection collection)
{
    int mes = Convert.ToInt32(collection["selectmes"]);
    return View();
}
    
answered by 03.01.2018 / 18:57
source