I have a question, how do I show an alert in MVC? I am validating from the controller the following:
var suscriptor = c.cargarDatosPDF(solicitud);
if (suscriptor.Count <= 0)
{
ViewBag.Alert = "Lo sentimos, esta solicitud no existe.";
return View("Index");
}
That list is loaded with data from SQL
. I would like that message to be seen in a PopUp
or Alert
, preferably a alert
. He Index
has a TextBox
and a Botón
.
I tried using:
alert(@ViewData["Mensaje"])
But, it hasn't worked. Any suggestion?
Update:
Index:
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Inicio</title>
<link rel="stylesheet" type="text/css" href="~/Estilos/inicio.css">
<script type="text/javascript" src="~/JavaScript/validar.js"></script>
<script type="text/javascript"></script>
</head>
<body>
<center>
<div class="topnav" style="width: 350px">
<a title="Providus S.A" href="http://www.providus.com.ar"><img src="~/Images/00.png" /></a>
</div>
<div style="width: 350px">
<form method="post" action="@Url.Action("Pdf", "Home")">
<label for="solicitud">Solicitud: </label>
<input type="text" id="solicitud" minlength="5" maxlength="7" name="solicitud" placeholder="Solicitud..." required
title="Sólo letras y números. Cantidad mínima de caracteres: 5. Cantidad máxima de caracteres: 7"
onkeypress="return soloLetrasYnumeros(event)">
<input type="submit" value="Buscar" />
</form>
</div>
</center>
@if (ViewBag.Alert != null)
{
<div class="alert alert-dismissable alert-info">
<button type="submit" class="close" data-dismiss="alert">
×
</button>
<strong>Providus informa...</strong> @ViewBag.Alert
</div>
}
@*<center>
<p class="mensaje">@ViewData["Mensaje"]</p>
</center>*@
</body>
</html>
First of all, the way you propose to change the message of the Javascript Alert box I inform you that it is not possible, if you want a personalized alert message you must look for an alternative, one can be using jQuery ( https://labs.abeautifulsite.net/ archived/jquery-alerts/demo/ )
Another and simpler way is to define in your View or in the _Layout (shared view (Shared) of all views) a ViewBag.Alert and printing the necessary message in each case.
And in your C# code you would pass the following message:
Another way is using a Modal ( https://getbootstrap.com/docs/4.0/components/modal/ ), where you would have to use some Javascript to do the sampling.
If what you want is a
alert()
simple javascript, the example that they have already given you is valid, making a small change. In the view, try adding this code:This code should be added to the end of the view.