Change URL Ajax Partialview Request

Asked

Viewed 450 times

2

I need to change the URL of the page according to the Partialview that I load via Ajax.

@EDIT

In addition, I also need to free direct access to Partialview via URL, but, bringing with you the page "father" because I work with it so that it performs functions of Masterpage, ie, I load in it scripts and layout. I wanted to avoid importing in each page my scripts.

Solved by adding $('#footer'). load('/Web/Footer'); to render a Partialview as footer

I have the following code in my View

 <div class="conteudo">
 <!-- Conteudo regular do site -->
 </div>
 <div id="corpoConteudo"></div>

via Ajax, I carry the content of PartialView within the <div id="corpoConteudo">

My script is this

    function Open(url) {
        Carregar();
        url = '@Url.Content("~/")' + url;
        $.ajax({
            url: url,
            type: 'GET',
            success: function (response) {
                $('#corpoConteudo').html(response);
                $('#loader').remove();
            },
            error: function () {
                alert('Ocorreu um erro!');
                $('#loader').remove();
            }
        });
    }

    function Carregar() {
        $('#corpoConteudo').append('<div id="loader"></div>');
    }
  • "To bring with you the parent page, "in my view, is to call the parent page. In MVC is not called the child content to bring the parent content, but the opposite. That’s precisely why Razor doesn’t have the concept of MasterPage, and yes, the concept of Layout. What pages do you need to call? Could you please improve your response?

  • Basically my Index page is treated as a Masterpage, because in it I give the header, Nav and footer. In the middle of it, in the div id="corpoConteudo", I bring my Partialview. For example: Case, News and so on. Accessing: http://localhost:3245/ I go straight to Index and then clicking on an element, navigate to Partialview. If access: http://localhost:3245/Web/Case, being my Web Controller and Case to Partialview, I do not load the elements (Header, Nav, Footer) because they are in my Index. I would like to do this against hand and also update the URL with Partialview that I carry.

  • So in my view this is an approach problem. The right one would be the Ajax carry another Action (that brings only the Partial), and the Actions use standard Views Father to load the Layout in full.

  • I understand and I agree with you. Furthermore, I edited my question with the solution I found to treat the problem the way the project is.

  • @Ciganomorrisonmendez, replacing the Partials with Views, it is no longer necessary to use a Partial that only loads the footer, because the footer will already be contained in the Layout. Using a Layout, for every Action call, will load the full page, so it is not causing items that have already been loaded to reload?

  • @Diegozanardo And what’s the problem in that? If I make a non-ajax request, I have to return the whole page anyway.

Show 1 more comment

1 answer

2


The best way to do this is to implement Actions that only answer Ajax. For this, implement an attribute that only responds Ajax, more or less like this:

public class AjaxOnlyAttribute : ActionMethodSelectorAttribute 
{
    public override bool IsValidForRequest(ControllerContext controllerContext, System.Reflection.MethodInfo methodInfo)
    {
        return controllerContext.RequestContext.HttpContext.Request.IsAjaxRequest();
    }
}

Decorate your Action as follows:

[AjaxOnly]
public ActionResult Rodape()
{
   ...
} 

Browser other questions tagged

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