How to use IEnumerable in knockout.js?
Problem Description
I wanted to ask about how to use a list Exbando Objects in knockout.js,am
using Rob Conrey's Massive and all returned results are dynamic, that's
fine with me it suits my needs but when it comes to sending results to
knockout i just don't know what to do with it.
Goal
Access object properties like obj.Name, obj.Brand etc...
Sample Code
View:
<div data-bind="foreach: Products">
<p>Product name: <strong data-bind="text: Name"></strong></p>
</div>
Controller:
public JsonResult GetProducts()
{
Products products = new Products();
var Model = products.GetAllProducts();
return Json(Model, JsonRequestBehavior.AllowGet);
}
The result of calling GetProducts is:
[[{"Key":"Id","Value":1},{"Key":"Name","Value":"Badass
Boots"},{"Key":"Brand","Value":"Nike"},{"Key":"Description","Value":"Super
cool boots that can make you fly (Not Really!)."}, etc...]]
Script File:
function ProductListViewModel() {
// Data
var self = this;
self.Products = ko.observableArray([]);
$.getJSON("/Home/GetProducts", function (data) {
self.Products(data);
});
}
No comments:
Post a Comment