Good morning, I hope you can help me with the following problem.
I am doing the CRUD of several catalogs, my back is in wcf and my front is in asp.net mvc. I added the service reference in my asp.net project and I already have the methods done. In the Controller I have two Update methods, in one the ws method is called to display the data and in the other the ws method is expected to be called to update that data.
Here is the controller code...
public ActionResult Update(int id)
{
if (Session["Usuario"] == null)
{
return RedirectToAction("Index", "Login");
}
else
{
ObtenerEdificio resp = obj.catEdificioObtener(id);
// Mostrar Update
return View(resp);
}
}
[HttpPost]
public ActionResult Update(ActualizaEdificio vm)
{
ActualizaEdificio entrada = new ActualizaEdificio();
entrada.Descripcion = vm.Descripcion;
entrada.Imagen = vm.Imagen;
RespuestaGeneral resp = obj.catEdificioActualiza(entrada);
ViewBag.successMessage = resp.Mensaje;
// Mostrar Index
return RedirectToAction("Index", "Edificio");
}
As you can see, each one calls a different method and model, so that's where the problem comes from. In my Update view I add the model ObtenerEdificio
and if the data is displayed, but I cannot add the model ActualizaEdificio
in the same view so the data cannot be edited.
Here is the code of my view...
@model AdministraZion.ServiceReference1.ObtenerEdificio
<div class="panel br-20x panel-default">
<div class="panel-head">
<div class="panel-title">
<i class="icon-layers panel-head-icon text-primary"></i>
<span class="panel-title-text">Editar Edificio</span>
</div>
</div>
@using (Html.BeginForm("Update", "Edificio", FormMethod.Post))
{
@Html.HiddenFor(d => d.IdEdificio)
<div class="box-body">
<div class="form-group">
<label for="inputDescripcion" class="form-control-label mt-3 ml-5">Nombre:</label>
@Html.ValidationMessageFor(m => m.Descripcion, "", new { @class = "ml-2", style = "color:red" })
@Html.TextBoxFor(m => m.Descripcion, "", new { @type = "text", @class = "form-control ml-5", style = "width:92%" })
</div>
<div class="box-footer" style="text-align:center">
<label for="file-upload" class="subir">
<i class="fas fa-cloud-upload-alt"></i> Subir archivo
</label>
<input class="block" id="file-upload" onchange='cambiar()' type="file" style='display: none;' name="Imagen" />
<div id="info"></div>
</div>
</div>
<div class="box-footer" style="text-align:center">
<button type="submit" class="btn block btn-success mt-3 mb-3" style="width: 30%" name="crear">Guardar <i class="fa fa-plus-circle"></i></button>
</div>
}
</div>
So I hope you can help me and tell me how I could do this part and if you have examples or sites that can support me. Thanks.
UPDATE
Here is the code of my WS...
public ObtenerEdificio catEdificioObtener(int idEdificio)
{
ObtenerEdificio resultado = new ObtenerEdificio();
try
{
using (SqlConnection con = new SqlConnection(strCon))
{
using (SqlCommand com = new SqlCommand("pa_catEdificioObtener", con))
{
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("@idEdificio", idEdificio);
con.Open();
using (SqlDataReader dr = com.ExecuteReader())
{
while (dr.Read())
{
resultado.IdEdificio = int.Parse(dr[0].ToString());
resultado.Descripcion = dr[1].ToString();
resultado.Imagen = Path.Combine(urlBaseImagenesSubidas, dr[2].ToString());
}
}
}
}
}
catch (Exception ex)
{
guardaLogGeneral("catEdificioObtener Datos", 0, string.Format("{0} , {1}, {2}", ex.Message, ex.InnerException, ex.StackTrace), "WS");
}
return resultado;
}
public RespuestaGeneral catEdificioActualiza(ActualizaEdificio cliente)
{
RespuestaGeneral resultado = new RespuestaGeneral();
try
{
using (SqlConnection con = new SqlConnection(strCon))
{
using (SqlCommand com = new SqlCommand("pa_catEdificioActualiza", con))
{
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("@idEdificio", cliente.IdEdificio);
com.Parameters.AddWithValue("@descripcion", cliente.Descripcion);
com.Parameters.AddWithValue("@nombreImagen", cliente.Imagen);
con.Open();
using (SqlDataReader dr = com.ExecuteReader())
{
while (dr.Read())
{
resultado.IdRespuesta = Convert.ToInt32(dr["idRespuesta"].ToString());
resultado.Mensaje = dr["mensaje"].ToString();
}
}
}
}
}
catch (Exception ex)
{
guardaLogGeneral("catEdificioActualiza", cliente.IdEdificio, string.Format("{0} , {1}, {2}", ex.Message, ex.InnerException, ex.StackTrace), "");
resultado.IdRespuesta = 0;
resultado.Mensaje = "Se produjo un error inesperado: " + ex.Message;
}
return resultado;
}
Here I leave the code of my models...
public class ActualizaEdificio
{
public int IdEdificio { get; set; }
public string Descripcion { get; set; }
public string Imagen { get; set; }
}
public class ObtenerEdificio
{
public int IdEdificio { get; set; }
public string Descripcion { get; set; }
public string Imagen { get; set; }
}
What you have to do is receive the viewmodel that you initially sent in the POST . When you show "Update" you use the GetBuilding viewmodel , when that view returns by POST in the parameters you put the same viewmodel and thus you will have the object with the loaded properties.