html显示undefined,undefined:$ undefined在成功返回结果后显示在cshtml页面中

我有一个简单的页面,它需要一个ID来从数据库返回的产品列表中找到特定产品,然后在html页面中显示此产品。我能够看到我使用/ api/products/12345时期望看到的JSON,但是当我尝试从Index.cshtml页面查询数据时,我找回了结果 - > undefined:$ undefined在我的页面中。我将通过Product类和我的html页面。请注意,所有产品的显示都完美呈现。

public class Product

{

public int ID { get; set; }

public string ProductDescription { get; set; }

public string UnitOfMeasure { get; set; }

public decimal MSRP { get; set; }

public string Category { get; set; }

public int CategoryID { get; set; }

public string ZipCode { get; set; }

}

这是我Index.cshtml页

.:: Web API ::.

$(document).ready(function() {

// Send an AJAX request - the second parameter is a callback function that is invoked when the request successfully completes.

$.getJSON("api/products/",

function (data) {

// On success, 'data' contains a list of products.

$.each(data, function (key, val) {

// Format the text to display.

var str = val.ProductDescription + ': $' + val.MSRP;

// Add a list item for the product.

$('

', { html: str }).appendTo($('#products'));

});

});

});

function find() {

var id = $('#prodId').val();

// Again, we call the jQuery getJSON function to send the AJAX request, but this time we use the ID to construct the request URI.

$.getJSON("api/products/" + id,

function (data) {

// On success, 'data' contains a list of products.

$.each(data, function (key, val) {

// Format the text to display.

var str = val.ProductDescription + ': $' + val.MSRP;

$('#products').html(str);

});

})

.fail(

function (jqXHR, textStatus, err) {

$('#products').html('Error: ' + err);

});

}

All Products

ID:

我遇到的问题是与查找()函数将呈现的UI有意义的数据,我看到那个数据匹配ID 12345已成功返回。

谢谢。

+0

在每个语句前添加一个调试器并检查json对象格式 –

2012-07-06 20:06:50

+0

我猜你的web方法在指定一个id时返回单个对象[不是数组],你能告诉你代码 –

2012-07-06 20:08:52

+0

不,我的web方法有一个填充List的GetAll,当我试图获得一个特定的产品时,我可以看到一个产品在我的调试器中返回。 –

2012-07-06 20:14:28

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值