I have a system in which I want to register an "Agent"
What I am looking for is to fill in the part SucursalEmpresa
with the information that is in its own table in a SelectList, but I cannot call two models in a single view.
This is my Agent model:
public class AgenteM
{
public string Agente { get; set; }
[Required(ErrorMessage = "El campo Nombre es obligatorio")]
public string Nombre { get; set; }
public string Familia { get; set; }
public string Tipo { get; set; }
public string Estatus { get; set; }
public int SucursalEmpresa { get; set; }
}
And this is my Branch model:
public class SucursalM
{
public int Sucursal { get; set; }
public string Nombre { get; set; }
}
This is my controller:
[HttpPost]
public IActionResult Guardar(AgenteM oAgente)
{
//Recibe el objeto para guardarlo en BD
if (!ModelState.IsValid)
return View();
var respuesta = _AgenteD.Guardar(oAgente);
if (respuesta == true)
return RedirectToAction("Listar");
else
return View();
}
where do I call my functionGuardar
public bool Guardar(AgenteM oAgente)
{
ListarSucursal();
bool rpta;
try
{
var cn = new Conexion();
using (var conexion = new SqlConnection(cn.GetCadenaSql()))
{
conexion.Open();
SqlCommand cmd = new SqlCommand("sp_WebCrearAgente", conexion);
cmd.Parameters.AddWithValue("Agente", oAgente.Agente);
cmd.Parameters.AddWithValue("Nombre", oAgente.Nombre);
cmd.Parameters.AddWithValue("SucursalEmpresa", oAgente.SucursalEmpresa);
cmd.Parameters.AddWithValue("Tipo", oAgente.Tipo);
cmd.Parameters.AddWithValue("Estatus", oAgente.Estatus);
cmd.Parameters.AddWithValue("Familia", oAgente.Familia);
cmd.CommandType = CommandType.StoredProcedure;
cmd.ExecuteNonQuery();
}
rpta = true;
}
catch(Exception ex)
{
string error = ex.Message;
rpta = false;
}
return rpta;
}
here I have a function to bring all the information of the branches:
public List<SucursalM> ListarSucursal()
{
var oLista = new List<SucursalM>();
var cn = new Conexion();
using (var conexion = new SqlConnection(cn.GetCadenaSql()))
{
conexion.Open();
SqlCommand cmd = new SqlCommand("sp_WebObtenerSucursales", conexion);
cmd.CommandType = CommandType.StoredProcedure;
using (var dr = cmd.ExecuteReader())
{
while (dr.Read())
{
oLista.Add(new SucursalM()
{
Sucursal = (int)dr["Sucursal"],
Nombre = dr["Nombre"].ToString()
});
}
}
}
return oLista;
}
And this is my view, where I want to fill a selectList with the information from the table.
<div class="row">
<div class="col-sm-6">
<label class="form-label">Agente</label>
<input asp-for="Agente" type="text" class="form-control" name="Agente">
</div>
<div class="col-sm-6">
<label class="form-label">Nombre</label>
<input asp-for="Nombre" type="text" class="form-control" name="Nombre">
<span asp-validation-for="Nombre" class="text-danger"></span>
</div>
<div class="col-sm-6">
<label class="form-label">Sucursal Empresa</label>
@*@Html.DropDownListFor(model => model.AgenteM.SucursalM.Sucursal, new SelectList(Model.AgenteM.SucursalM.Sucursales, "Sucursal", "Nombre"), "Selecciona una Sucursal", new { @class = "form-control", @id = "txtSucursal", @placeholder = "Sucursal" })*@
<input asp-for="SucursalEmpresa" type="text" class="form-control" name="SucursalEmpresa">
</div>
<div class="col-sm-6">
<label class="form-label">Tipo</label>
<input asp-for="Tipo" type="text" class="form-control" name="Tipo">
</div>
</div>
What is wrong with me or what am I doing wrong? I hope you can help me.
You can't pass 2 models to the view.
What can be done is:
For cases where the view is composed of 2 or more models, it is necessary to create a class that contains all the models.
For cases like yours, where it is only to fill a select, what is normally used is a ViewBag or a ViewData
In the action that generates your view you place
The first parameter is the list, the second the name of the property that acts as ID, and the second the name of the property that acts as value
There is a 4th parameter to indicate the selected one, which is used when you edit an element that already exists, that is, in the Edit view
Then in your view you put:
Or if you want to create it freehand:
on controller
In the view