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();
}
}
}