How to make the server return a page without the browser request

1

I have a registration screen that must be validated by several business rules, when the user submits the submit I wanted to leave a page with a message, for example, "Validating, please wait". I tried doing this with asynchronous delegates, I did a test project just to test if it would work, the logic on the server even worked, it called a method that put a sleep on the thread to symbolize the validation time and called the method that brings the The waiting screen problem is that the screen does not appear despite being called. see the controller code

public delegate int Completo();

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    //chama o metodo trabalhar que simula a espera e coloca a action ValidacaoCompleto para ser executada quando o metodo
    //Trabalhar estiver concluído
    public void Validacao()
    {
        Numero n = new Numero();
        Completo c = new Completo(n.Trabalhar);
        IAsyncResult result = c.BeginInvoke(new AsyncCallback (ValidacaoCompleto), null);
        TelaValidacao();
    }

    //chama a View de espera
    public ActionResult TelaValidacao()
    {
        return View();
    }

    //esse metodo chama a View de validação concluida, não chamei a View direto no metodo TelaValidacao 
    //por que o tipo de retorno do metodo que será chamdo pelo delegate assincrono não pode ser Actionresult
    public void ValidacaoCompleto(IAsyncResult result)
    {
        ChamarTelaFinal();
    }

    //chama a View de validação concluida
    public ActionResult ChamarTelaFinal()
    {
        return View();
    }

}
    
asked by anonymous 29.09.2014 / 16:23

2 answers

1

I'll focus on what I think is the key point of your question:

  

(...) I wanted to leave a page with a message for example, "Validating, please wait ...".

I'm not an MVC expert. I think if you force the load of some page on the server side, what might be happening is that the one who made the request is server side code, and who will get the answer is the same.

If all you want is a load screen, ideally do this on the client side. You make the Ajax request and at the same time put some animated gif or anything indicating that there is some processing going on on the server.

When you get a response from the Ajax request, you handle the response and display a result accordingly.

A little bit of pseudo-code to illustrate:

In XHTML:

<asp:Hidden runat="server" clientIDMode="static" id="resultado" />

No code-behind:

public void Processa () {
    bool deuTudoCerto;
    /* .. SNIP .. */
    resultado.Value = deuTudoCerto.ToString();
}

And in Javascript (assuming you use jQuery):

var animacaoProcessamento = $("#processamento");
$.ajax({
    url: enderecoDaPaginaQueProcessaResultado
}).done(function (
    var deuTudoCerto = $("#resultado").val() === "true";
    alert(deuTudoCerto ? "Deu tudo certo!" : "Ops, falhou...");
    animacaoProcessamento.fadeOut();
));
animacaoProcessamento.fadeIn();
    
29.09.2014 / 18:40
1

I solved the problem with Ajax.BeginForm, where the request is sent via Ajax and I mark the contents of one div to be copied to another after the submit action, so I can make the contents of a hidden div be displayed in a div that was empty, and this content could be a code with the message waiting.

@using (Ajax.BeginForm("Validacao", "Home", new AjaxOptions { UpdateTargetId = "resultado", LoadingElementId = "carregando" }))
{
    <input type="submit" value="Iniciar" />
}


<div id="carregando" style="display:none">
    <center>
        <h1><strong>Validação em andamento, aguarde...</strong></h1>
    </center>
</div>

<div id="resultado">

</div>
    
29.09.2014 / 20:16