ViewBag到达空 [英] ViewBag arrives empty
问题描述
我有一个在数据库中注册了聊天的表格,一切正常,问题在于ViewBag,因为这会将消息带到视图中的Javascript以便进行验证.当用户注册时,应该显示一条成功消息,但是如果显示了用户数据,则ViewBag会在数据库中将其显示为错误,因此唯一的问题就是ViewBag.
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.
控制器
//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");
}
查看
@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>
推荐答案
要回答您的问题,我首先要说的是,在您的情况下,为 ViewBag
获取空值实际上是正确的,因为您使用了 RedirectToAction
将取消所有 ViewBag
数据
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
ViewBag
的寿命是当前请求,这意味着一旦您尝试离开当前请求,该请求将变为null.因此,您需要根据自己的情况使用 TempData
.
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
是一种仅存储会话存储方式的数据.这是两次请求之间的最后期限.它有助于将数据从一个控制器传输到另一个或从一个动作传输到另一个.您使用 TempData
的方式与使用View data或ViewBag的方式有些不同.
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.
第一
使用TempData和ViewBag
在发布"操作中,将最后一行更改为
In the Post action change the finally line to
finally
{
TempData["message"] = message;
}
然后在您要重定向到的Get操作中,因为您使用了RedirectToAction,请获取TempData并将其传递给view bag
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;
注意 需要if语句来处理初始的get请求,即在未设置TempData时不设置ViewBag的值
第二
仅使用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
这篇关于ViewBag到达空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!