How to do a back post (refresh) on MVC

Asked

Viewed 1,559 times

1

I am new to MVC and am used to web Forms and the events of the controls that cause postback and update the page.

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

In the code below I managed to call the method I need but it does not postback (reflesh) the page automatically, either using $post or using $ajax

To update the page I am using Location.Reload(); which only does postBack(reflesh) at the end of $post or $ajax execution.

I need an "effect" equal to an "Asp:button" that onClick already flashes the screen on time and executes on the server the code c#.

Well I hope I got the right information out of what 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();
    });
});

  • I think that in the MVC architecture you should call the controler, sending the viewmodel, the controler will do what has q be done with the viewmodel and return the view (which may be the same) with a new viewmodel, this view can be worked with this new object, gave to understand?

  • I could understand what you meant, but I don’t know how to do it.

1 answer

2


I think it’s worth explaining the architectural differences between Web Forms and MVC. You could start here, but I’m going to dig a little deeper into the concepts you’ve put.

I am new to MVC and am used to web Forms and the events of the controls that cause postback and update the page.

The postback does not exist in MVC. This is because architecture includes state representation (REST, as we call it, and we’re not talking about REST API and Web API, but rather the definition of REST). The postback came at a time when a Web Forms application does not implement this. You have the page and the Code Behind interacting with the page, no matter how many events the page and the Code Behind have in common.

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

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

Yes, precisely because this is out of the question. You want to make an Ajax pagination, but reload the location. Makes no sense.

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

Secondly, the callback success just reload the page. It’s a useless effort. You don’t populate anything. It doesn’t change anything on screen. Using Ajax is discontinued.

Try this paging package on View static. It’s better to follow this path you started.

I need an "effect" equal to an "Asp:button" that onClick already flashes the screen on time and executes on the server the code c#.

You better ask another question asking for Ajax paging for ASP.NET MVC.

  • 1

    Thanks @Gypsy Marrison Mendez, I still hadn’t commented on your reply because I didn’t understand it so well when you published it, now how I improved the language I can already understand it better.

Browser other questions tagged

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