Problem with AJAX

Asked

Viewed 85 times

0

I am developing a site in ASP.NET MVC, I wanted that when I changed screen only be updated my div .ESTRUTURA here is the code:

Javascript:

function loadPage(page) {
    $.ajax({
        type: 'GET',
        url: page,
        dataType: 'html',
        cache: true,
        async: true,
        success: function (data) {
            console.log($('.ESTRUTURA').html(data));
            $('.ESTRUTURA').html(data);
        }
    });
}

HTML (In this case it is the _Layout.cshtml)

<body>
    <div class="bg">
        @Html.Partial("_Header")
        <div class="ESTRUTURA"> 
            @RenderBody()
        </div>
        @Html.Partial("_Footer")
    </div>
    <script src="~/Scripts/jquery-1.8.2.min.js"></script>
    <script src="~/Scripts/vendors/underscore.js"></script>
    <script src="~/Scripts/vendors/backbone.js"></script>
    <script src="~/Scripts/app.js"></script>
</body>

Inside my DIV .ESTRUTURA has the @RenderBody(), I believe the problem is there. The variable data, receives the entire HTML of the page (but should not).

  • So your problem is in the construction of the variable data. You must review what you are getting in your request ajax

  • The variable receives the return of @Renderbody(), I was able to fix this problem by changing the return of the controllers to Partialview(), but the first page I load (Home), has to be View(), otherwise the other content of the page like Header Footer and navbar

  • That’s right, your home has to be a view. However I see your problem... The variable data receive the return of the Renderbody??

  • Yes, that in the case when I put Partialview(), the Renderbody() only and returns content, but when I put View() it returns me the whole page, I need him to only run the Home View() the first time, the others need to be like Partialview()

1 answer

2


Do it like this, it can work in two ways.

The first is by making the action just return to partial view, that is, the view without the master page (_Layout.cshtml).

Controller (First form)

public ActionResult MinhaAction() {
   return PartialView();
}

But we also have a problem, if the user decides to access the URL directly will only return the partial part, in which case I would work as below.

Controller (Second form)

public ActionResult MinhaAction() {
   return View();
}

[HttpPost, ActionName("MinhaAction")]
public ActionResult MinhaActionPartial() {
  return PartialView();
}

And you should change the request method to POST instead of GET, so will make every request POST returns the partial part and GET the full page.

Jquery

function loadPage(page) {
    $.ajax({
        type: 'POST',
        url: page,
        dataType: 'html',
        cache: true,
        async: true,
        success: function (data) {
            console.log($('.ESTRUTURA').html(data));
            $('.ESTRUTURA').html(data);
        }
    });
}
  • Thank you, Diego, it worked perfectly.

Browser other questions tagged

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