How to do a postback (refresh) in MVC

1

I am new to MVC and am accustomed to web forms and the events of the controls that cause the postback and refresh the page.

I need to make this update on the MVC page after a javascript is triggered.

In the code below I was able to call the method I need but it does not do the postback (reflesh) of the page automatically, neither using $ post nor using $ ajax

To update the page I'm using location.reload (); which only does the postback (reflesh) only at the end of running $ post or $ ajax.

I need an "effect" just like an "asp: button" that onClick already flashes the screen on time and runs the C # code on the server.

Well I hope I have been able to get the right information I need.

<script type="text/javascript">

var totalPaginas = parseInt('@ViewBag.TotalPaginas');
var paginaAtual = parseInt('@ViewBag.PaginaAtual');
var registrosPorPagina = parseInt('@ViewBag.RegistrosPorPagina');

$('#divPaginacaoUm').bootpag({

    total: totalPaginas,
    page: paginaAtual,
    maxVisible: registrosPorPagina,
    leaps: true,
    firstLastUse: true,
    first: '<',
    last: '>',
    wrapClass: 'pagination',
    activeClass: 'active',
    disabledClass: 'disabled',
    nextClass: 'next',
    prevClass: 'prev',
    lastClass: 'last',
    firstClass: 'first'

}).on("page", function (event, num) {

      $.ajax({
        type: 'POST',
        url: '@Url.Action("Paginacao", "Pesquisa")',
        data: { 'num': num },
        dataType: 'html',
        cache: false,
        async: true,
        success: function (data) {
            location.reload();
        }
     });

    $.post('@Url.Action("Paginacao", "Pesquisa")', { num: num }, function (data) {
        location.reload();
    });
});

    
asked by anonymous 30.10.2015 / 18:35

1 answer

2

I think it's worth explaining the architectural differences between Web Forms and MVC.

  

I am new to MVC and am accustomed to web forms and the events of the controls that cause the postback and refresh the page.

postback does not exist in MVC. This is because the architecture contemplates representation by states ( REST, as we call , and we are not talking about REST API and Web API, but rather from the definition of REST). The postback came at a time when a Web Forms application did not implement this. You have the page and Code Behind interacting with the page, however many events the page and Code Behind have in common.

In MVC, there is no such interaction between page and code. The View page is the result of a request that has already ended. What you do, as in the case of Ajax, is to open new requests, but the server code is not "turned on the page" all the time as it is in Web Forms.

  

In the code below I was able to call the method I need but it does not automatically refresh the page, neither using $ post nor using $ ajax.

Yes, precisely because this is out of purpose. You want to do Ajax pagination, but reload location . Does not make sense.

First, ASP.NET MVC has a paging component . He does this job for you.

Second, callback success only reloads the page. It's a futile effort. You do not populate anything. Does not change anything on screen. The use of Ajax is deprecated.

Try to test this paging package in a static View . You better follow this path you started.

  

I need an "effect" just like an "asp: button" that onClick already flashes the screen on time and runs the C # code on the server.

You'd better ask another question asking for Ajax pagination for ASP.NET MVC.

    
10.12.2015 / 16:43