javascript - why isn't jscript displaying my items? -
i have following javascript:
$(document).ready(function () { $.getjson("api/products/", function (data) { $.each(data, function (key, val) { // format text display. var str = val.name + ': $' + val.price; // add list item product. $('<li/>', { text: str }) .appendto($('#products')); }); }); });
my problem product list not displaying. have function can search individual items in list , able display those, why isn't working?
here's class information stored in:
public class product { public int id { get; set; } public string name { get; set; } public string category { get; set; } public decimal price { get; set; } }
and here's controller:
public class productscontroller : apicontroller { product[] products = new product[] { new product { id = 1, name = "tomato soup", category = "groceries", price = 1 }, new product { id = 2, name = "yo-yo", category = "toys", price = 3.75m }, new product { id = 3, name = "hammer", category = "hardware", price = 16.99m } }; public ienumerable<product> getallproducts() { return products; } public product getproductbyid(int id) { var product = products.firstordefault((p) => p.id == id); if (product == null) { throw new httpresponseexception(httpstatuscode.notfound); } return product; } public ienumerable<product> getproductsbycategory(string category) { return products.where( (p) => string.equals(p.category, category, stringcomparison.ordinalignorecase)); } }
i'm beginner asp.net please let me know i'm doing wrong.
i think meant use text method off initial jquery selector creates <li> node.
passing second parameter $() result in "context" attempt limit selector starts from. see: http://api.jquery.com/jquery/#jquery1/
$(function() { var data = [{ name: 'test1', price: 1000}, { name: 'test2', price: 25}]; $.each(data, function (key, val) { // format text display. var str = val.name + ': $' + val.price; // add list item product. $('<li/>') .text(str) .appendto($('#products')); }); });
Comments
Post a Comment