I am trying to retrieve a list of objects via jQuery Ajax
I have a controller Method like this:
[HttpGet]
public JsonResult GetAllStates(string listname = "")
{
clsDdl ddl = new clsDdl();
List<clsDdl> retval = new List<clsDdl>();
ddl.id = "1";
ddl.value = "Dropdown1";
ddl.status = "DDL Status";
ddl.attributes = "First Dropdown Text";
retval.Add(ddl);
//return retval;
return Json(new
{
list = retval
}, JsonRequestBehavior.AllowGet);
}
Heres my Dropdown class that I am trying to return
public class clsDdl
{
public string id { get; set; }
public string value { get; set; }
public string status { get; set; }
public string attributes { get; set; }
}
Code in my view looks like this
function LoadListItems (options) {
var listname = options.listname;
$.ajax({
type: "GET",
url: url,
data: JSON.stringify({
listname: listname
}),
contentType: "application/json; charset=utf-8",
async: true,
success: function (result) {
alert(result[0].value);
},
error: function (xhr, status, err) {
alert(err.toString(), 'Error - LoadListItemsHelper');
}
});
My controller action is being hit. But the alert is always 'Undefined'. I have also tried
success: function (data) {
var result = data.d;
for (var i = 0; i < result.length; i++) {
alert(result[i].attributes);
}
No Success there either. What am I doing wrong ?
Thanks in advance...