Pfvitor, good afternoon. From what I understand, you want a popup that displays information.
You can directly use a javascript component called Alert in the view, as follows:
<script>
$(document).ready(function(){
alert("Você não pode fazer uma nova solicitação pois ainda possui solicitações em aberto!");
});
</script>
The above code used in a view will create an alert with the message in parameter as soon as the page loads.
However, from what I’m seeing in your code, you want to perform a business rule handling on your controller and send the message to the view to present an error.
If this is really the scenario, you can use the mvc Validationsummary component to present the errors on the screen you set up in the controller. Your code would look like this
Controller
[HttpPost]
public ActionResult NovaSolicitacao(Solicitacao pedidoSolicitacao)
{
pedidoSolicitacao.Usuario_Id = GetUser();
if (_data.Usuarios.GetByID(GetUser()).Items.Any(x => x.Data == null && x.Status == 1))
{
ModelState.AddModelError("", "Você não pode fazer uma nova solicitação pois ainda possui solicitações em aberto");
return RedirectToAction("Index", "Home");
}
....
}
And then, in the view that performs the Submit for the code above, would be:
@using (Html.BeginForm())
{
@Html.ValidationSummary
@*Todo o conteudo do formulario*@
}
Validation Summary will display errors of your model and those you add to Modelstate in Controller.
If you have created an Empty project, it will come without Jquery Unobstrusive, which is responsible for validating the View. To add it, you can refer to the following answer:
/a/31785/4539
Victor, you can also send information to the view via the Tempdata dictionary.
Your code would look like this:
[HttpPost]
public ActionResult NovaSolicitacao(Solicitacao pedidoSolicitacao)
{
pedidoSolicitacao.Usuario_Id = GetUser();
if (_data.Usuarios.GetByID(GetUser()).Items.Any(x => x.Data == null && x.Status == 1))
{
TempData["mensagemErro"] = "Você não pode fazer uma nova solicitação pois ainda possui solicitações em aberto";
return RedirectToAction("Index", "Home");
}
....
}
In the view, you can display as you wish, as an example below. In this example, it checks for an error message, and if it exists, the error is displayed in a field-error class div.
@if(TempData["mensagemErro"] != null)
{
<div class="field-error" >
@TempData["mensagemErro"]
</div>
}
Dude, I’m going to put it as a comment because I don’t know if it would be a good answer. But to make pop up appear, you have to do it right in the view. As far as I know, it’s the view that makes use of them.
– Érik Thiago
And can you tell me how to do it by @Érikthiago ?
– PFVictor
You use bootstrap as a library in your project ?
– Érik Thiago
No use @Érikthiago ...
– PFVictor
You can use jquery in addition to the friend answer
– Érik Thiago