2

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
0

2 Answers 2

3

I used result[0].Value to get the value and result[0].Text to get the text property.

halfer
20.2k20 gold badges111 silver badges208 bronze badges
answered Nov 9, 2011 at 22:03
Sign up to request clarification or add additional context in comments.

Comments

1

you should try result[0].SelectListItem.Value/Text or other property

answered Nov 9, 2011 at 21:55

1 Comment

wait i check and revert, i have mistaken it is returning array of SelectListItem so result[0].Valiue/Text should work

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.