title: How to inject model collection into backbone.js from asp.net mvc?
body: <p>I've seen examples in backbone.js where it says the initial model collection should be bootstrapped into the page instead of going out to fetch it. That point makes sense. For some reason, I cannot figure out how to do it using an asp.net mvc application. I started a quick example below.</p>
  
  <p>Controller Action: </p>
  
  <pre><code>public ActionResult Index()  
  {
      CustomerRespository repository = new CustomerRespository();
  
      ViewModel model = new ViewModel();
      model.Customers = repository.GetAll();           
  
      return View(model);
  }
  </code></pre>
  
  <p>View Model: Here I am creating the json needed to inject my customer list into the app.</p>
  
  <pre><code>public List&lt;Customer&gt; Customers { get; set; }
  public string CustomerJson
  {
      get
      {
          JavaScriptSerializer serializer = new JavaScriptSerializer();                
          return serializer.Serialize(this.Customers);        
      }
  }
  </code></pre>
  
  <p>Decoding the json in my view:</p>
  
  <pre><code>@{ string s = HttpUtility.HtmlDecode(Model.CustomerJson); }
  </code></pre>
  
  <p>Calling collection.reset() in backbone.js app:</p>
  
  <pre><code>this.customers = new CustomerCollection();
  this.customers.reset("@s");
  </code></pre>
  
  <p>For some reason this does not seem to work correctly. </p>