2
我在backbone.js中看到了一些示例,它说初始模型集合应该引导到页面中,而不是去取出它。这一点是有道理的。出于某种原因,我无法弄清楚如何使用asp.net mvc应用程序来做到这一点。我在下面开始了一个快速示例。如何将模型集合从asp.net mvc注入到backbone.js中?
控制器动作:
public ActionResult Index()
{
CustomerRespository repository = new CustomerRespository();
ViewModel model = new ViewModel();
model.Customers = repository.GetAll();
return View(model);
}
视图模型:在这里,我要创造我的注入客户名单到应用程序所需要的JSON。
public List<Customer> Customers { get; set; }
public string CustomerJson
{
get
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
return serializer.Serialize(this.Customers);
}
}
解码在我看来,JSON:)在Backbone.js的应用
@{ string s = HttpUtility.HtmlDecode(Model.CustomerJson); }
调用collection.reset(:
this.customers = new CustomerCollection();
this.customers.reset("@s");
出于某种原因,这似乎并不能正常工作。
完美。非常感谢!!! – Frankie 2012-02-12 17:39:35