views:

45

answers:

2

I'm preparing an application I wrote in ASP.Net MVC for some light Ajax-y stuff. Basically, I want users to be able to go to different parts of the page without it reloading.

To do this I want to be able to reload the body of my page.

My site master is broken down into a head body and foot div. I want to be able to use something like $("body").load(whateverlink) to refresh the body, but in my case doing this causes the website to be rendered inside the body.

How can I do this successfully?

EDIT: The page is made up of views, not partialviews. In theory I could go and convert all my pages to partials but I'm looking for a way around that if possible. Thanks

+2  A: 

You can get your controllers to return partial views, using JQuery to do a request and update a div with your page content:

JQuery:

function navigate(url){
  $.ajax({
    url: "/Home/Index",
    cache: false,
    success: function(html){
       $("#content").html(html);
      }
   });
}

Controller:

public class HomeController : Controller
{    
    public void Index()
    {
       return this.PartialView();
    }
}
Charlie
I tried this, however (technically worse) result. Now it renders a second copy of the page right below the first one. I there something I'm forgetting? If I didn't mention it, I'm trying to load a new view not partialview
edude05
@edude05 I've changed $("#content").append(html) to $("#content").html(html). That should overwrite the content of your div rather than appending the html
Charlie
Thank Charlie, but so far that gives me the same result as (main).load(/controller/action). I need a way to get ASP to return my view as a partial, right now its still returning the entire page inside the div.
edude05
Is your partial view a user control? It needs to be an ASCX file.
Charlie
A: 

There is a helpful article on BrightMix which might help. It basically renders a partial to a string, which you could then return as part of a ContentResult.

Dan Atkinson