I have a form where a Chat will be registered in the database, everything works fine, the problem is the ViewBag, since it carries the message to Javascript in the view so that it can place a validation. When the user registers, a success message should appear, but the ViewBag makes it appear as an error, in the database if the user's data is displayed, so the only problem would be the ViewBag.
Also, when I run it again, the modal just appears fine with the message success.
Controller
//Charlas
public ActionResult CrearCharla()
{
List<ClsSede> listaSede = new List<ClsSede>();
ClsSede Sede1 = new ClsSede();
Sede1.sede_Text = "LIMA - SAN BORJA";
Sede1.sede_Value = "LIMA - SAN BORJA";
ClsSede Sede2 = new ClsSede();
Sede2.sede_Text = "LIMA - LOS OLIVOS";
Sede2.sede_Value = "LIMA - LOS OLIVOS";
ClsSede Sede3 = new ClsSede();
Sede3.sede_Text = "LIMA - CHORRILLOS";
Sede3.sede_Value = "LIMA - CHORRILLOS";
listaSede.Add(Sede1);
listaSede.Add(Sede2);
listaSede.Add(Sede3);
ViewBag.Sedes = new SelectList(listaSede, "sede_Text", "sede_Value");
return View(new ClsCharla());
}
[HttpPost]
public ActionResult CrearCharla(ClsCharla charla)
{
List<ClsSede> listaSede = new List<ClsSede>();
ClsSede Sede1 = new ClsSede();
Sede1.sede_Text = "LIMA - SAN BORJA";
Sede1.sede_Value = "LIMA - SAN BORJA";
ClsSede Sede2 = new ClsSede();
Sede2.sede_Text = "LIMA - LOS OLIVOS";
Sede2.sede_Value = "LIMA - LOS OLIVOS";
ClsSede Sede3 = new ClsSede();
Sede3.sede_Text = "LIMA - CHORRILLOS";
Sede3.sede_Value = "LIMA - CHORRILLOS";
listaSede.Add(Sede1);
listaSede.Add(Sede2);
listaSede.Add(Sede3);
ViewBag.Sedes = new SelectList(listaSede, "sede_Text", "sede_Value", charla.sede_Charla);
//-----
string mensaje = "";
try
{
ClsConexion con = new ClsConexion();
var Cnx = con.Conexion();
OracleCommand cmd = new OracleCommand("SIMEXA_SP_REGISTRAR_CHARLA", Cnx);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new OracleParameter("param_titulo", OracleDbType.Varchar2)).Value = charla.titulo_Charla.Trim();
cmd.Parameters.Add(new OracleParameter("param_descrip", OracleDbType.Varchar2)).Value = charla.descrip_Charla.Trim();
cmd.Parameters.Add(new OracleParameter("param_fecha", OracleDbType.Varchar2)).Value = charla.fecha_Charla;
cmd.Parameters.Add(new OracleParameter("param_hora", OracleDbType.Varchar2)).Value = charla.hora_Charla;
cmd.Parameters.Add(new OracleParameter("param_lugar", OracleDbType.Varchar2)).Value = charla.lugar_Charla.Trim();
cmd.Parameters.Add(new OracleParameter("param_sede", OracleDbType.Varchar2)).Value = charla.sede_Charla;
cmd.Parameters.Add(new OracleParameter("param_requisito", OracleDbType.Varchar2)).Value = charla.requisito_Charla.Trim();
Cnx.Open();
OracleTransaction tx = Cnx.BeginTransaction();
cmd.ExecuteNonQuery();
tx.Commit();
Cnx.Close();
cmd.Dispose();
Cnx.Dispose();
mensaje = "exito";
}
catch
{
mensaje = "error";
}
finally
{
ViewBag.mensaje = mensaje;
}
return RedirectToAction("MostraCharlas");
}
View
@model wsCharlas.Models.ClsCharla
@{
ViewBag.Title = "Crear Charla";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Crear Charla:</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<h4>Aquí usted podrá colocar todos los datos respectivos de la charla que desee crear.</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<!-- <div class="form-horizontal"> -->
<div class="form-group">
@Html.HiddenFor(model => model.ID_Charla, htmlAttributes: new { @class = "control-label" })
<div>
@Html.HiddenFor(model => model.ID_Charla, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ID_Charla, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.titulo_Charla, htmlAttributes: new { @class = "control-label" })
<div>
@Html.EditorFor(model => model.titulo_Charla, new { htmlAttributes = new { @class = "form-control", maxlength = "40" } })
@Html.ValidationMessageFor(model => model.titulo_Charla, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.descrip_Charla, htmlAttributes: new { @class = "control-label" })
<div>
@Html.TextAreaFor(model => model.descrip_Charla, new { @id = "textArea", @class = "form-control", maxlength = "530" })
@Html.ValidationMessageFor(model => model.descrip_Charla, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.fecha_Charla, htmlAttributes: new { @class = "control-label" })
<div>
@Html.EditorFor(model => model.fecha_Charla, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.fecha_Charla, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.hora_Charla, htmlAttributes: new { @class = "control-label" })
<div>
@Html.EditorFor(model => model.hora_Charla, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.hora_Charla, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.lugar_Charla, htmlAttributes: new { @class = "control-label" })
<div>
@Html.EditorFor(model => model.lugar_Charla, new { htmlAttributes = new { @class = "form-control", maxlength = "100" } })
@Html.ValidationMessageFor(model => model.lugar_Charla, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.sede_Charla, htmlAttributes: new { @class = "control-label" })
<div>
@Html.DropDownListFor(model => model.sede_Charla, (SelectList)ViewBag.Sedes, "Seleccione una Sede", new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.sede_Charla, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.requisito_Charla, htmlAttributes: new { @class = "control-label" })
<div>
@Html.EditorFor(model => model.requisito_Charla, new { htmlAttributes = new { @class = "form-control", maxlength = "100" } })
@Html.ValidationMessageFor(model => model.requisito_Charla, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2">
<input type="submit" value="Crear Charla" id="id_charla" class="btn btn-success" />
</div>
</div>
<!--</div> -->
}
<div>
@Html.ActionLink("Regresar", "MostraCharlas", null, new { @class = "btn btn-primary" })
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
<style>
#textArea{
min-height: 62px;
max-height: 135px;
}
</style>
<script>
var msg = '@ViewBag.mensaje'
$("#id_charla").on("click", function () {
if (msg == 'exito') {
Swal.fire(
msg,
'Se agrego una nueva charla!',
'success'
)
} else {
Swal.fire(
msg,
'No se pudo registrar su nueva charla, asegúrese de completar todo el formulario, si el problema continua comuniquese con el área de informática!',
'error'
)
}
});
</script>
What is happening to you is that you are doing a redirect, and by doing this your ViewBag is not maintained. That is, the ViewBag becomes null when you make a redirect or a second call.
What you need is to put that value in the TempData and in your second action method you retrieve it, something like: