title: Loading MVC 4 default view with jQuery load()
body: <p>I have built an mvc 4 website, and I built it so that the main layout page doesn't refresh if a different section is loaded with jQuery. I put the navigator and jQuery script in _Layout.cshtml:</p>
  
  <pre><code>&lt;ul id="menu" class="menu-items"&gt;
                  &lt;li&gt;&lt;a id="Item1" href="#" onclick="loadPage(this.id)"&gt;Item1&lt;/a&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;a id="Item2" href="#" onclick="loadPage(this.id)"&gt;Item2&lt;/a&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;a id="Item3" href="#" onclick="loadPage(this.id)"&gt;Item3&lt;/a&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;a id="Item4" href="#" onclick="loadPage(this.id)"&gt;Item4&lt;/a&gt;&lt;/li&gt;
              &lt;/ul&gt;
  
  &lt;/body&gt;
  
  &lt;script&gt;
  
      function loadPage(action) {
  
          $.post("/Home/" + action, function (data) {
              $(content).html(data);
          });
      }
  &lt;/script&gt;
  </code></pre>
  
  <p>Then I have my controller:</p>
  
  <pre><code>namespace MyApp.Controllers
  {
  public class HomeController : Controller
  {
  
      public ActionResult Index()
      {
          return View();
      }
  
      [HttpPost]
      public ActionResult Item1()
      {
          if (Request.IsAjaxRequest())
          {
              return PartialView();
          }
          return View();
      }
  
      [HttpPost]
      public ActionResult Item2()
      {
          if (Request.IsAjaxRequest())
          {
              return PartialView();
          }
          return View();
      }
  </code></pre>
  
  <p>Etc, etc.</p>
  
  <p>Everything works fine, except that I don't know how to use just one main content view (which is index.cshtml when the website loads in the browser). I'm forced to put the same content that's in index.cshtml into item1.cshtml so that when I trigger onlick for item1, it will go back to the main content. The only route config I have is for the Default, which initially set to Index:</p>
  
  <pre><code>public static void RegisterRoutes(RouteCollection routes)
      {
          routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
  
          routes.MapRoute(
              name: "Default",
              url: "{controller}/{action}/{id}",
              defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
          );
      }
  </code></pre>
  
  <p>What I want, is to be able to use just one main content page, but have the ajax call still get me back to the main content when I click Item1. Does anyone know what I need to do? It seems to be a little overkill to have to update both views when I want to update the main content.</p>
  
  <p>Also, I think other web devs will like this code. Especially if you're building a band's website like I'm doing. It allows me to put the demo song media player in the _layout.cshtml page so that it won't refresh when the user is clicking to the other sections (i.e. if it refreshes, the media player stops). With this design, the user can navigate the whole website while the songs continue to play.</p>
  
  <p>I'm rather new to javascript, so I'm sure I could have made a better onclick handler rather than using anchor tags, so if anyone want to show me a better way, please do. But my main problem is the index.cshtml vs item1.cshtml dilemma.</p>