0

I have a pretty standard MVC solution with _Layout.cshtml, Index.cshtml, etc. I'm trying to load some required bundles in the Layout only one time, so that when I change views, I only get what I need from the server, not what I already have (bundles, layout html, etc).

Following this post: MVC 3: How to render a view without its layout page when loaded via ajax?

I now have an updated _ViewStart. It doesn't seem to be working though:

With the following JS viewmodel being loaded as part of the bundle in _Layout:

var subjectservice = new baseservice();  // baseservice is an empty function
subjectservice.subjects = {};
subjectservice.getSubjects = function () {
    alert('Hit');
    subjectservice.subjects = 'data';
    }
subjectservice.getSubjects();

The intended behavior is that:

  • I get Subjects when I initially load the application (with _Layout), no matter what page I'm on
  • I don't call getSubjects again when navigating - only when I specifically call it from other viewmodels

However, no matter what page I navigate to, I always get the alert message, even if Layout has already loaded. Viewing the network panel in Chrome Debugger shows all of my bundled js files are getting reloaded with every page load.

An example of a view that I'm trying to load without re-loading bundles and _Layout:

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

How can I load Layout and its bundles only once?

Community
  • 1
  • 1
RobVious
  • 12,685
  • 25
  • 99
  • 181

1 Answers1

2

What I believe you're referring to is the ability to use Ajax and Pushstate.

Luckily there's a great OSS lib for this.

https://github.com/defunkt/jquery-pjax

Do the following to your _ViewStart.cshtml

@{ 
    Layout = Request.Headers["X-PJAX"] != null ? 
             "~/Views/Shared/_PjaxLayout.cshtml" : 
             "~/Views/Shared/_Layout.cshtml";  // uses the _Layout.cshtml for unsupported browsers
}

More info here
http://chrisseroka.wordpress.com/2012/04/24/getting-starteg-with-pjax-and-asp-net-mvc/

Chase Florell
  • 46,378
  • 57
  • 186
  • 376
  • I want to keep this as out-of-the-box as possible. MVC *has* to have some mechanism to handle this. – RobVious Oct 10 '13 at 18:55
  • it's not the way it works. you either create a partial request, or a full request. a partial request is done how I described before, and a full request is what you're doing now. – Chase Florell Oct 10 '13 at 19:01
  • MVC is simply a pattern. It doesn't have client side anything baked in... Your "MVC" app is running on a server. – Chase Florell Oct 10 '13 at 19:02
  • What does the post I linked to solve then (I'm curious, not being a dick)? I know what partials are, I just don't want to make two methods for each view depending on whether or not Layout is loaded. Looking for a clean, non-obtrusive solution without introducing bloat into the project, if that exists. – RobVious Oct 10 '13 at 19:07
  • I wasn't referring to partial views, I was referring to partial page loads and postbacks. PJAX is seriously the best way. You'll have two different Layout pages, and then develop everything as though you only have a standard _Layout. The link you pointed to is also doing AJAX partial postbacks... similar to pjax. pjax just does it better with MINIMAL intervention. In fact you can plug PJAX into an existing MVC app that was never designed for partial postback/page loads, and it'll be working in a matter of minutes. – Chase Florell Oct 10 '13 at 19:10
  • Am I right in feeling paranoid that using PJAX will negatively affect the crawlability of my site? – RobVious Oct 10 '13 at 19:58
  • don't be paranoid. Because the crawlers wont send the `["X-PJAX"]` header, and therefore nothing will be partially loaded... it will rely on your standard layout page. – Chase Florell Oct 10 '13 at 20:01
  • Thanks for the info chase. I'm going to play with this but want to hold out on selecting an answer in case there's a purer way to achieve what I'm looking for. I really want to avoid adding yet-another-framework, even if it means a bit more complexity on implementation. – RobVious Oct 10 '13 at 20:09
  • Still trying to get PJax working... if you have a minute to take a look at a *third* question of mine today, here it is: http://stackoverflow.com/questions/19306116/pjax-not-working-with-mvc-project – RobVious Oct 10 '13 at 20:59