11

I am calling my controller method using .ajax. my controller method call web service which returns dictionary. now i need to return this and populate dropdown list. i am trying with return JSON and need to populate using success (response)

I am using MVC 1.0

        $.ajax(
            {
                url: 'LookupValue/',
                data: { 'sLookupIds': selectedtext },
                datatype: "json",
                traditional: true,
                success: function (data) {
                    alert(data.value);
                }
            });

thanks in advance.

2 Answers 2

20

In controller

    public JsonResult LookupValue(String sLookupIds)
    {

        SelectList olist = new SelectList(oDict, "key","value");

        return Json(olist);

  }

In view

        $.ajax(
            {
                url: 'LookupValue/',
                data: { 'sLookupIds': selectedtext },
                datatype: "json",
                traditional: true,
                success: function (data) {
                    $.each(data, function (index, val) {
                        $('#lookup')
                        .append($("<option></option>")
                        .attr("value", val.Value)
                        .text(val.Text));
                        //ddHTML = ddHTML + "<option  value='" + val.Value + "'>'" + val.Texts + "'</option>";
                    });
                }
            });
Sign up to request clarification or add additional context in comments.

1 Comment

Instead of JsonResult and Json you can use ActionResult and return SelectList. That way you can use content negotiation...
0

In your Action in your Controller:

return Json(data);

Where data is your object that you want serialiazed to JSON.

If you want to use Json.NET, just override the Json method.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.