views:

61

answers:

2

Hi

I have in my c# code a class with 3 properties.

public class Sender
{
   public string Id {get; set;}
   public string html {get; set;}
   public string AnotherField {get; set}
}

So I have a list collection.

List<Sender> test = new List<Sender>();

So I return this as a Json result in my view

public Json myView()
{
   return Json(test);
}

so now in my jquery ajax post request I want to grab that response and go through this collection.

So far I got a jquery each loop but I need get like the "id" to use in my jquery. So I need to call the records key values. How could I do this?

A: 

Don't know jQuery but in javascript in general:

var sender = get_JSON_object_somehow();
// dot notation:
var id = sender.Id;
var html = sender.html;
var anotherfield = sender.AnotherField;

// square bracket notation
// if you have the key in another variable
// or if your key is a reserved keyword in JS:
var key = "Id";
var id = sender[key];
var foo = sender['this']; // 'this' is reserved
slebetman
+1  A: 

You say you want to get the "id" property, but in your Sender class at the server-side you declared an Id property (uppercase I), you should note that JavaScript is case-sensitive:

$.getJSON("/Senders/GetSenders", function(data) {
 $.each(data, function(i, obj) { // iterate the serialized JSON list
  alert(obj.Id);
 });
});
CMS