Load variables depending on selected item MVC C #

0

Inquiry

Good, I would like to save variables by default after selecting an item in a select (to then insert them into the database). In my case the select "State of work" (which is loaded by call to database). The idea is that depending on the selected item, variables with values are returned to be stored in database.

For example,

  • if I select 1, that returns the variables number1 = 12 and description="twelve"
  • if I select 3, that returns the variables number2 = 13 and description="thirteen"

Thank you in advance

VISTA

         Index     

    <fieldset>
        <legend>Ingrese Datos</legend>

        <table>
            <tr>
                <td><label>Código Obra</label></td>
                <td><label>Nombre Obra</label></td>
                <td><label>Año Obra</label></td>
                <td><label>Cota</label></td>
                <td><label>Caudal Diseño</label></td>
            </tr>
            <tr>
                <td><input type="text" name="txtCodObra" maxlength="10" id="txtCodObra" /></td>
                <td><input type="text" name="txtNomObra" maxlength="10" id="txtNomObra" /></td>
                <td><input type="number" name="txtanyoObra" min="1900" max="2050" maxlength="4" id="txtanyoObra" /></td>
                <td><input type="text" name="txtCota" maxlength="10" id="txtCota" /></td>
                <td><input type="text" name="txtCaudal" maxlength="10" id="txtCaudal" /></td>

            </tr>
            <tr>
                <td><label>Estado de la Obra</label></td>
                <td><label>Cota Cámara</label></td>
            </tr>
            <tr>
                <td>
                    <select name="e_u" id="e_u">
                        <option value="0">Seleccione Estado</option>
                        @{
                            if (ViewData["estados"] != null)
                            {
                                IEnumerable<dynamic> estados = (IEnumerable<dynamic>)ViewData["estados"];

                                foreach (dynamic est_uso in estados)
                                {
                                    string nombreestado = est_uso.Nombreestado;
                                    int e_u = est_uso.idestado;
                                    <option value="@est_uso.idestado"> @nombreestado.ToUpper() </option>

                                }

                            }
                       //dependiendo esta seleccion, cargar variable     
                        }
                    </select>

                </td>
                <td><input type="text" name="txtCotaCam" maxlength="10" id="txtCotaCam" /></td>
            </tr>
            <tr>
                <td>Capacidad Operativa</td>
            </tr>
            <tr>
                <td><input type="text" name="txtCapOperativa" maxlength="10" /></td>
            </tr>
        </table>

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


</form>

CONTROLLER

    [ActionName("Index")]
    [HttpGet]
    public ActionResult GetIndex()
    {
        NameValueCollection queryString = Request.QueryString;
        IDbConnection database = new SqlConnection(config.ConnectionString);
        string query = @"
        select 
            l.cod_local 'CodigoLocalidad',
            l.nom_local 'NombreLocalidad'
        from 
                localidad l  
        ";


        IEnumerable<dynamic> estados = database.Query(query1);
        ViewData["estados"] = estados;
        return View();
    }

    [ActionName("Index")]
    [HttpPost]
    public ActionResult PostIndex(FormCollection form,  int e_u)
    {

// The idea is that depending on the selection, return certain variables

        int cond = e_u;

        if (cond != 3)

        {
            string vigencia = "vigente";

        }
        else 
        {
            string vigencia = "No vigente";

        }

        string codobra = form["txtCodObra"];
        string nobra = form["txtNomObra"];
        string aobra = form["txtAnyoObra"];
        string cota = form["txtCota"];
        string cota_cam = form["txtCotaCam"];
        string caudal = form["txtCaudal"];
        string capop = form["txtCapOperativa"];


        IDbConnection database = new SqlConnection(config.ConnectionString);
        Fam201 f = new Fam201();

        f.CodObra = codobra;
        f.NomObra = nobra;
        f.AnyoObra = int.Parse(aobra);
        f.Cota = float.Parse(cota);
        f.CotaCam = float.Parse(cota_cam);
        f.Caudal = float.Parse(caudal);
        f.CapOperativa =float.Parse(capop);
        f.IdEst = e_u;


        int? id = database.Insert(f);

        string query = @"
        select 
            l.cod_local 'CodigoLocalidad',
            l.nom_local 'NombreLocalidad'
        from 
                localidad l  
        ";


        IEnumerable<dynamic> estados = database.Query(query1);
        ViewData["estados"] = estados;


        ViewData["notificacion"] = @"Registro Agregado, Codigo Obra:" + codobra + ", Nombre Obra: " + nobra + ", Año " + aobra + ".";

        return View();
    }
}

}

    
asked by koxe_24 08.01.2018 в 22:25
source

1 answer

0

The situation is that an html option-value element only sends the value property, and not the content / text. For what I recommend you generate your option-value so that you send the two data and in the action method you separate it:

View

<select name="e_u" id="e_u">
<option value="0">Seleccione Estado</option>
@{
    if (ViewData["estados"] != null)
    {
        IEnumerable<dynamic> estados = (IEnumerable<dynamic>)ViewData["estados"];

        foreach (dynamic est_uso in estados)
        {
            string nombreestado = est_uso.Nombreestado;
            int e_u = est_uso.idestado;
            <option value="@est_uso.idestado:@nombreestado.ToUpper()"> @nombreestado.ToUpper() </option>
        }
    }
}
</select>

Controller

[HttpPost]
public ActionResult Index(string e_u)
{
    int IdEst = Convert.ToInt32(e_u.Split(':')[0]);
    string DescEst = e_u.Split(':')[1];
}
    
answered by 09.01.2018 в 00:24