I am trying to pass JsonResult into my jquery ajax, in my JS:
$.ajax({
contentType: 'application/json, charset=utf-8',
type: "POST",
url: "/Controller/TestJson",
cache: false,
dataType: "json",
success: function (result) {
alert(result.length);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert('error');
}
});
in my Controller I have:
public JsonResult TestJson ()
{
List<SelectListItem> list = new List<SelectListItem>() {
new SelectListItem() { Value = "1", Text = "VA" },
new SelectListItem() { Value = "2", Text = "MD" },
new SelectListItem() { Value = "3", Text = "DC" }
};
return this.Json(list);
}
When I run it, the length is 3, but if I do something like alert (result[0]), I get [Object object]...so it looks like Json(list) doesn't jsonify it....
What am I doing wrong here?
I used result[0].Value
to get the value and result[0].Text
to get the text property.