- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个在数据库中注册聊天的表单,一切正常,问题是 ViewBag,因为这会将消息带到 View 中的 Javascript,以便它可以进行验证。当用户注册时,应该会出现一条成功消息,但在 ViewBag 中它显示为错误,如果显示了用户的数据,则在数据库中,所以唯一的问题是 ViewBag。
此外,当我再次重新运行时,带有成功消息的模态显示得很好。
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");
}
查看
@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
获取 null 实际上是正确的,因为您使用了 RedirectToAction
这将使所有 ViewBag
数据
ViewBag
的生命周期是当前请求,这意味着一旦您尝试离开当前请求,它就会变为空。所以你需要在你自己的情况下使用 TempData
。
TempData
是一种按照 session 存储方式存储的数据。它的生命周期持续在两个请求之间。它有助于将数据从一个 Controller 传输到另一个 Controller 或从一个 Action 传输到另一个 Action 。您使用 TempData
的方式与使用 View data 或 ViewBag 的方式略有不同。
所以我给你两个选择。
第一
使用 TempData 和 ViewBag
在 Post 操作中将 finally 行更改为
finally
{
TempData["message"] = message;
}
然后在你重定向到的Get action中,因为你使用了RedirectToAction,获取TempData并将它传递给view bag
var message = TempData ["message"];
If(message != null)
ViewBag.message = message;
注意 需要 if 语句来处理初始 get 请求,即在未设置 TempData 时不设置 ViewBag 的值
第二
仅使用 TempData
finally
{
TempData["message"] = message;
}
无论您决定使用哪个,访问方式都与您分配它的方式相同,
TempData["message"] //for TempData
ViewBag.message //for ViewBag
关于c# - ViewBag 空空如也,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57063952/
我有一个在数据库中注册聊天的表单,一切正常,问题是 ViewBag,因为这会将消息带到 View 中的 Javascript,以便它可以进行验证。当用户注册时,应该会出现一条成功消息,但在 ViewB
我是一名优秀的程序员,十分优秀!