I have a form where a Chat is registered in the database, everything works fine, the problem is the ViewBag, since this takes the message to Javascript in the view so that it can place a validation. When the user registers, a success message should appear, but by the ViewBag it appears as an error, in the database if the user's data is displayed, so the only problem would be the ViewBag.
Also, when I re-run again, the modal with the message success appears just fine.
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 message = "";
try
{
ClsConexion con = new ClsConexion();
var Cnx = con.Conexion();
OracleCommand cmd = new OracleCommand("SIMEXA_SP_REGISTER_CHAT", 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();
message = "success";
}
catch
{
message = "error";
}
finally
{
ViewBag.message = message;
}
return RedirectToAction("MostraCharlas");
}
View
@model wsCharlas.Models.ClsCharla
@{
ViewBag.Title = "Create a Chat";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Create a Chat:</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<h4>Here you can place all the respective data of the chat you want to create.</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, "Select a Headquarters", 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="Create a Chat" id="id_charla" class="btn btn-success" />
</div>
</div>
<!--</div> -->
}
<div>
@Html.ActionLink("Return", "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.message'
$("#id_charla").on("click", function () {
if (msg == 'success') {
Swal.fire(
msg,
'A new chat was added!',
'success'
)
} else {
Swal.fire(
msg,
'Could not register your new chat, be sure to complete the entire form, if the problem continues to communicate with the computer area!',
'error'
)
}
});
</script>
To answer your question, I will start by saying that getting null for the ViewBag
in your case is actually correct, because you used RedirectToAction
which will nullify all ViewBag
data
The lifespan of a ViewBag
is the current request, which means that once you attempt to leave the current request, it becomes null. So you need to use TempData
in your own case.
TempData
is a data stored just the way sessions are stored. It's lifespan last between two request. It helps the transfer of data from one controller to another or from one action to another. You use TempData
in a bit different way from the way View data or ViewBag is used.
So I will give two options for you.
First
Use TempData and ViewBag
In the Post action change the finally line to
finally
{
TempData["message"] = message;
}
Then in the Get action you are redirecting to, because you used RedirectToAction, get the TempData and pass it to view bag
var message = TempData ["message"];
If(message != null)
ViewBag.message = message;
Note the if statement is needed to handle initial get requests, that is not setting the value of the ViewBag when the TempData is not set
Second
Use only TempData
finally
{
TempData["message"] = message;
}
Whichever you decide to use, is accessed the same way you assigned it,
TempData["message"] //for TempData
ViewBag.message //for ViewBag
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.