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

Asked

Viewed 499 times

1

I have a registration screen that should be validated by various business rules, when the user does Submit I wanted to leave a page with a message for example, "Validating, wait". I tried to do this with asynchronous delegates, I did a test project just to test if it would work, the logic on the server even worked, He called a method that puts a Sleep in the thread to symbolize the validation time and called the method that brings the wait screen the problem is that the screen does not appear, despite being called. Check 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();
    }

}

2 answers

1


Let me focus on what I think is the key point of your question:

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

I’m no expert on MVC. I believe that if you force the load of some server-side page, what may be happening is that whoever made the request is server-side code, and who will receive the answer is the same.

If all you want is a charge screen, the ideal would be to do it on the client’s side. You make the request Ajax and at the same time puts some gif animated or anything that indicates that there is some processing taking place on the server.

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

A short stretch of pseudo-code to illustrate:

In XHTML:

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

In the 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();
  • really, using ajax is simpler. In ASP.NET MVC has the ajax.beginform method that sends the form all via ajax, I will post the full code of how I did.

1

I solved the problem with Ajax.Beginform, where the request is sent via Ajax and I mark the content of a div to be copied to another after the action of Ubmit, so I can make the content of a hidden div to be displayed in a div that was empty, and this content can 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>

Browser other questions tagged

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