Ich möchte eine Nachricht zu meiner Ansicht von meinem Controller anzeigen, aber es passiert nichts. Ich verwende ViewBag.Message, um die Nachricht anzuzeigen. Ich weiß nicht, ob ich einen Fehler in meinem Code mache oder es ist nicht richtig, es zu tun. Dies ist mein Code:Nachricht anzeigen
Controller:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id_IngresoM,Id_Componente,Lote,Serie,Cantidad,Id_Usuario")] IngresoMateriales ingresoMateriales)
{
var user = "1ef69472-1b7d-460d-a6f9-9d458c5e314e";
string msj = "";
try
{
var affectedRows = db.Database.ExecuteSqlCommand("IngresoMaterialesInspeccion @IdComponente, @Lote, @Serie, @Cantidad, @IdUsuario",
new SqlParameter("@IdComponente", ingresoMateriales.Id_Componente),
new SqlParameter("@Lote", ingresoMateriales.Lote),
new SqlParameter("@Serie", ingresoMateriales.Serie),
new SqlParameter("@Cantidad", ingresoMateriales.Cantidad),
new SqlParameter("@IdUsuario", user));
//ModelState.AddModelError("", "El Certificado no esta Vigente");
if (affectedRows == '3')
{
msj = "El Certificado no esta Vigente";
}
else if (affectedRows == '2')
{
msj = "El Componente esta libre de Inspeccion";
}
else if (affectedRows == '1')
{
msj = "Componente Sospechoso";
}
else
{
msj = "Pues nada";
}
ViewBag.Message = msj;
return RedirectToAction("Create");
}
catch (SqlException ex)
{
foreach (SqlError Error in ex.Errors)
{
return new JavaScriptResult { Script = Error.ToString() };
}
}
ViewBag.Message = msj; ------------------------------
return View();
}
Ausblick:
<div class="form-group">
@Html.LabelFor(model => model.Serie, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<input class="form-control" type="text" id="Serie" name="Serie" onkeypress="FunctionS(event)" />
@Html.ValidationMessageFor(model => model.Serie, "", new { @class = "text-danger" })
</div>
</div>
@if (ViewBag.Message != null)
{
<script type="text/javascript">
window.onload = function() {
alert("@ViewBag.Message"); ---------------------
};
</script>
}
</div>
Es hat keinen Sinn, "ViewBag.Message" einen Wert zuzuweisen, wenn Sie dann umleiten. –