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?
asked Nov 9, 2011 at 21:51
sarsnake
28.1k62 gold badges185 silver badges295 bronze badges
2 Answers 2
I used result[0].Value to get the value and result[0].Text to get the text property.
Sign up to request clarification or add additional context in comments.
Comments
you should try result[0].SelectListItem.Value/Text or other property
answered Nov 9, 2011 at 21:55
Amritpal Singh
1,7851 gold badge13 silver badges20 bronze badges
1 Comment
Amritpal Singh
wait i check and revert, i have mistaken it is returning array of SelectListItem so result[0].Valiue/Text should work
lang-js