javascript - Accessing fields in a JSON response -



javascript - Accessing fields in a JSON response -

i have class defined this:

// widget class has lots of properties don't want display // utilize displaywidget class carry want display public class displaywidget { public string widgetid {get; set;} public string description {get; set;} public displaywidget(widget widget) { widgetid = widget.widgetid; description = widget.description; } }

i have actionresult method ends with::

var widgets = new list<displaywidget>(); foreach (var widget in matchingwidgets) { widgets.add(new displaywidget(widget)); } homecoming json(widgets);

my problem is, don't know how access widgetid , description properties within of ajax .done handler:

.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 })); }); } )

what should within of .each function output widgetid , description?

your actionresult returning array, try:

element[0].widgetid

this homecoming first result, can loop through list if need be.

edit

as @stephenmuecke mentioned, don't need utilize json.parse here returning json info suffice loop through results:

.done( function(response) { $('#widgetsection').html(''); $.each(response, function (index, element) { $('body').append( $('<div></div>').text(element.widgetid ) ) }); } )

javascript asp.net-mvc json asp.net-ajax

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -