Alert/pop up message in ASP.Net MVC

Asked

Viewed 5,901 times

1

How to create Alert or pop up to have the same effect as MessageBox in a web application? My code

[HttpPost]
public ActionResult NovaSolicitacao(Solicitacao pedidoSolicitacao)
{
    pedidoSolicitacao.Usuario_Id = GetUser();
    if (_data.Usuarios.GetByID(GetUser()).Items.Any(x => x.Data == null && x.Status == 1))
    {
        MessageBox.Show("Você não pode fazer uma nova solicitação pois ainda possui solicitações em aberto!");           
        return RedirectToAction("Index", "Home");
    }
    ....
}

Local works that way but I saw that the MessageBox does not work for web applications.

How to replace this then to appear a little window on the screen with this message?

  • 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.

  • And can you tell me how to do it by @Érikthiago ?

  • You use bootstrap as a library in your project ?

  • No use @Érikthiago ...

  • You can use jquery in addition to the friend answer

1 answer

5


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>
}
  • Please avoid long discussions in the comments; your talk was moved to the chat

  • 1

    Using Tempdata is the best way, it worked right here in my!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.