title: Rich web app using ASP.NET MVC and Backbone.js and progressive enhancement
body: <p>I'm working on a small web app using mvc and backbone.js and I have a couple of thoughts about how to handle async request vs regular requests.</p>
  
  <p>Today I use a controller called /pages which returns a partial view if it's a ajax request and a standard view if it's a regular request. In <a href="https://stackoverflow.com/questions/9367957/backbone-router-creates-multiple-views-which-causes-multiple-events-to-bind-to-t">another question</a> I was told I'm doing it all wrong when I send a bunch of HTML back to the client.</p>
  
  <p>So how should I structure my controllers etc to handle both async and non async requests?</p>
  
  <p>In my case I have the following code in my pages controller</p>
  
  <pre><code>public ActionResult Index() {
      var id = _model.Id;
      var parentId = _model.Parent != null ? _model.Parent.Id : null;
      var viewModel = new IndexViewModel
      {
          RootModel = _session.Query&lt;IPageModel&gt;().SingleOrDefault(model =&gt; model.Parent == null),
          CurrentModel = _model,
          ParentModel = parentId != null ? _session.Load&lt;IPageModel&gt;(parentId) : null,
          Children = _session.Query&lt;IPageModel&gt;()
              .Where(model =&gt; model.Parent.Id == id)
              .Where(model =&gt; !model.Metadata.IsDeleted)
              .OrderBy(model =&gt; model.Metadata.SortOrder)
              .ToList()
      };
  
      if(Request.IsAjaxRequest()) {
          return PartialView(viewModel);    
      }
      return View(viewModel);
  }
  </code></pre>
  
  <p>But if I understand things correctly I would be better off sending back a collection of pages instead of a complete view model? How should I handle this in my controller?</p>
  
  <p>Is it a good idea to create a separate controller/api using eg. the api controller in mvc 4?</p>