【发布时间】:2014-11-08 16:42:56
【问题描述】:
我有一个这样定义的类:
// the Widget class has lots of properties I don't want to display
// so I use the displayWidget class to carry just what I want to display
public class displayWidget
{
public string WidgetId {get; set;}
public string Description {get; set;}
public displayWidget(Widget widget)
{
WidgetId = widget.WidgetId;
Description = widget.Description;
}
}
我有一个以:: 结尾的 ActionResult 方法
var widgets = new List<displayWidget>();
foreach (var widget in matchingWidgets)
{
widgets.Add(new displayWidget(widget));
}
return Json(widgets);
我的问题是,我不知道如何访问 ajax .done 处理程序中的 WidgetId 和 Description 属性:
.done(
function(response) {
$('#widgetSection').html('');
var html = '';
var jsonData = JSON.parse(response);
$.each(jsonData,
function (index, element)
{
$('body').append($('<div>', { text: element.WidgetId }));
$('body').append($('<div>', { text: element.Description }));
});
}
)
.each 函数中应该包含什么来输出 WidgetId 和 Description?
【问题讨论】:
-
返回什么 element.WidgetId/element.Description?
-
我不知道...我知道响应包含我期望的内容,但现在 JSON.parse(response) 返回错误 0x800a03f6 - JavaScript 运行时错误:无效字符
-
不要'用户 JSON.parse。试试 $.each(response,function(){});
标签: javascript asp.net-mvc json asp.net-ajax