1

I'm trying to bind a drop down using .json file.

My ActionMethod:

 public JsonResult LoadDropdown()
    {
        using (StreamReader sr = new StreamReader(Server.MapPath("~/Scripts/Drop1.json")))
        {
            var users = JsonConvert.DeserializeObject<List<Item>>(sr.ReadToEnd());
            return Json(users, JsonRequestBehavior.AllowGet);
        }

    }

My AJAX:

$.ajax({
        type: "GET",
        url: "/Home/LoadDropdown",            
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
            var ddlCustomers = $("[id*=drop1]");
            ddlCustomers.empty().append('<option selected="selected" value="0">--Please select--</option>');
            //alert(data.d);
            $.each(data.d, function () {
                ddlCustomers.append($("<option></option>").val(this['id']).html(this['name']));
            });                
        },
        error: function (jqXHR, textStatus, errorThrown) {
            alert(textStatus + "_" + errorThrown);
        }
    });        

My action method returns the following JSON Objects,

enter image description here

Now in Ajax I'm getting 'Undefined' when I checked 'data.d' before loop. Also 'data' holding objects like - [object]. Can anyone please help me what is the mistake?

2
  • 2
    Its just $.each(data, function (index, item) { ddlCustomers.append($("<option></option>").val(item.id).text(... Commented May 26, 2016 at 5:43
  • From where this data.d came? Commented May 26, 2016 at 6:09

2 Answers 2

3

Try the following:

$.each(data, function (i,v) {
     ddlCustomers.append($("<option</option>",{value:v.id,text:v.name});
});
Sign up to request clarification or add additional context in comments.

1 Comment

Its data, not data.d
-1

My Action Mathod

            DataTable dt = ds.Tables[0];
            System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
            Dictionary<string, object> row;
            foreach (DataRow dr in dt.Rows)
            {
                row = new Dictionary<string, object>();
                foreach (DataColumn col in dt.Columns)
                {
                    row.Add(col.ColumnName, dr[col]);
                }
                rows.Add(row);
            }
            return serializer.Serialize(rows);

AJAX

            $.ajax({
            type: "POST",
            url: "GMCAddEditProduct.aspx/GetDropDownValue",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (response) {
                $("#ContentPlaceHolder1_ddlprotype").empty().append('<option selected="selected" value="0">--Select Product--</option>');
                $.each(JSON.parse(response.d), function (i, v) {
                    $("#ContentPlaceHolder1_ddlprotype").append($("<option></option>", { value: v.ProductTypeID, text: v.Name }))
                });
              },
        });

Comments

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.