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?

0

2 Answers 2

3

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

1

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

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

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.