Alert / pop up message in ASP.Net MVC

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");
    }
    ....
}

Location works this way but I have seen that MessageBox does not work for web applications.

How to replace this so that a little window appears on the screen with this message?

    
asked by anonymous 25.09.2014 / 16:48

1 answer

5

PFVitor, good afternoon. As I understand it, 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 parameterized message as soon as the page loads.

However, from what I see in your code, you want to perform a business rule handling on your controller and send the message to the view so that it displays an error.

If this is indeed the scenario, you can use the mvc ValidationSummary component to display the errors on the screen that you configure 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 submits to the code above, it would look like:

@using (Html.BeginForm())
{
    @Html.ValidationSummary
    @*Todo o conteudo do formulario*@
}

Validation summary will display the errors of your model and those you add to Modelstate on the Controller.

If you have created an Empty project, it will come without Jquery Unobstrusive, responsible for doing the validation in View. To add it, you can see the following answer:

  

link

Victor, you can also send information to the view through the TempData dictionary.

Your code looks 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 shown 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>
}
    
25.09.2014 / 19:52